Get your own free workspace
View
 

ODS2XMLPy

Page history last edited by PBworks 4 years, 3 months ago

## With thanks to http://www.linuxjournal.com/article/9347
import zipfile
from xml.dom.minidom import parseString

if __name__ == '__main__' :
        import sys
        if len(sys.argv) < 2 or len(sys.argv) > 3 :
                print "Usage : %s </path/to/file.ods> [</path/to/output.xml>]" %(sys.argv[0])
                exit
        outfile = "content.xml"
        if len(sys.argv) == 3 :
                outfile = sys.argv[2]
                infile = sys.argv[1]
        else :
                infile = sys.argv[1]

        output = open(outfile, "w")
        file = zipfile.ZipFile(infile)
        for name in file.infolist() :
                if name.orig_filename == 'content.xml' :
                        bh = file.read(name.orig_filename)
                        doc = parseString(bh)
# there are rows
                        rows = doc.getElementsByTagName('table:table-row')
                        for row in rows :
# each row has a columns
                                output.write("<row>n")
                                cols = row.childNodes
                                for col in cols :
# each col has a cell
                                        output.write("t<column>n")
                                        cells = col.childNodes
                                        for cell in cells :
# each cell has some text
                                             texts = cell.childNodes
                                             for text in texts :
                                                  output.write("tt<value>n")
                                                  if text.nodeType == text.TEXT_NODE :
                                                       output.write("ttt")
                                                       output.write(text.nodeValue.encode('utf-8'))
                                                       output.write("n")
                                                  output.write("tt</value>n")
                                        output.write("t</column>n")
                                output.write("</row>n")
        output.close()

Comments (0)

You don't have permission to comment on this page.