~openerp-commiter/openobject-addons/extra-6.0

« back to all changes in this revision

Viewing changes to document_webdav_old/webdav/odt2txt.py

  • Committer: Fabien Pinckaers
  • Date: 2008-12-12 09:09:57 UTC
  • Revision ID: fp@tinyerp.com-20081212090957-cson0n0jove7dt7i
document_webdav

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys, zipfile, xml.dom.minidom
 
4
import StringIO
 
5
 
 
6
class OpenDocumentTextFile :
 
7
        def __init__ (self, filepath) :
 
8
                zip = zipfile.ZipFile(filepath)
 
9
                self.content = xml.dom.minidom.parseString(zip.read("content.xml"))
 
10
 
 
11
        def toString (self) :
 
12
                """ Converts the document to a string. """
 
13
                buffer = u""
 
14
                for val in ["text:p", "text:h", "text:list"]:
 
15
                        for paragraph in self.content.getElementsByTagName(val) :
 
16
                                buffer += self.textToString(paragraph) + "\n"
 
17
                return buffer
 
18
 
 
19
        def textToString(self, element) :
 
20
                buffer = u""
 
21
                for node in element.childNodes :
 
22
                        if node.nodeType == xml.dom.Node.TEXT_NODE :
 
23
                                buffer += node.nodeValue
 
24
                        elif node.nodeType == xml.dom.Node.ELEMENT_NODE :
 
25
                                buffer += self.textToString(node)
 
26
                return buffer
 
27
 
 
28
if __name__ == "__main__" :
 
29
        s =StringIO.StringIO(file(sys.argv[1]).read())
 
30
        odt = OpenDocumentTextFile(s)
 
31
        print odt.toString().encode('ascii','replace')