float converted to string in ods

Asked by Peter Veltman on 2014-02-20

Hello,

I just started using POD. I have created an *.ods template and
managed to get a result.ods from the renderer, which fills desired
fields.

Now the problem is that the fields are filled with something like '2014
and not the number 2014. According to your website this should happen,
so could you point me to why this is going wrong?

I have a dataDict = { 'year': 2014 } and in the ods: ="year".

Kind Regards,

Peter

Question information

Language:
English Edit question
Status:
Solved
For:
Appy Edit question
Assignee:
No assignee Edit question
Last query:
2014-03-05
Last reply:
2014-06-23
davide moro (davide-moro) said : #1

Hi Peter,
I experienced the same issue.

Have you found a workaround for this?

Best regards,

davide

Peter Veltman (p-veltman) said : #2

Yes, I've created my own parser/generator.

## code ## (no further help)
import zipfile
import xml.dom.minidom

dataDict = {'$month':'oktober',
                     '$year':'2014',
                     '$value': 9.0} ## or any other string that you've defined in the template

outfile = "output.ods"
## Read template
tempFile = zipfile.ZipFile('template.ods', mode='r')
tempFileList = tempFile.infolist()

## Create outputFile
outFile = zipfile.ZipFile(outfile, mode='w')
for tFile in tempFileList:
    if (tFile.orig_filename!='content.xml'):
        stringFile = tempFile.read(tFile.orig_filename)
        outFile.writestr(tFile.orig_filename,stringFile)

## Now search and replace content in template and write this to outFile
contentFile = tempFile.read('content.xml')
doc = xml.dom.minidom.parseString(contentFile)
cells = doc.getElementsByTagName('table:table-cell')
#print "I have ", len(cells), "cells "
for cell in cells:
    cellData = ''
    for i in range(0,cell.attributes.length):
        ## Find the cells that are strings, check if the text:p element data is equal to a dict.key
        if(cell.attributes.item(i).value=='string'):
            ##print cell.attributes.item(i).name,cell.attributes.item(i).value
            nodeList = cell.getElementsByTagName('text:p')
            for p in nodeList:
                for ch in p.childNodes:
                    if ch.nodeType == ch.TEXT_NODE:
                        ##print ch.data
                        cellData = ch.data
                        if cellData in dataDict.keys():
                            #print "before: ",cell.toxml()
                            #print cellData
                            ch.replaceData(0,len(cellData),dataDict[cellData])
                           ## next lines are not nice, I make a difference between string outputs and floats in the XML file
                           ## So I check here for the dict.keys() which don't have string values
                            if (cellData!='$month' and cellData!='$year'):
                                cell.setAttribute('office:value-type','float')
                                cell.setAttribute('office:value',str(dataDict[cellData]))
    #print "after: ",cell.toxml()
## Then write to content.xml.
newContentFile = doc.toxml().encode('utf-8')
outFile.writestr('content.xml',newContentFile)
print "Saved data to", outfile

davide moro (davide-moro) said : #3

Thank you for sharing!

Instead for who needs a solution without having to write their own parser here it is a non techical workaround:
* add a hidden cell with ="myfloatvar" (for example E7)
* put a formula on the cell you want to display with =VALUE(E7) if you need a float or =INT(E7) for integers

Launchpad Janitor (janitor) said : #4

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

See the related bug.