[FIX]Web: Refixed the issue of context propagation, we must not remove search_default...
[odoo/odoo.git] / addons / document / odt2txt.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 ##############################################################################
4 #    
5 #    OpenERP, Open Source Management Solution
6 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
20 #
21 ##############################################################################
22
23 import sys, zipfile, xml.dom.minidom
24 import StringIO
25
26 class OpenDocumentTextFile :
27     def __init__ (self, filepath):
28         zip = zipfile.ZipFile(filepath)
29         self.content = xml.dom.minidom.parseString(zip.read("content.xml"))
30
31     def toString (self):
32         """ Converts the document to a string. """
33         buffer = u""
34         for val in ["text:p", "text:h", "text:list"]:
35             for paragraph in self.content.getElementsByTagName(val) :
36                 buffer += self.textToString(paragraph) + "\n"
37         return buffer
38
39     def textToString(self, element):
40         buffer = u""
41         for node in element.childNodes :
42             if node.nodeType == xml.dom.Node.TEXT_NODE :
43                 buffer += node.nodeValue
44             elif node.nodeType == xml.dom.Node.ELEMENT_NODE :
45                 buffer += self.textToString(node)
46         return buffer
47
48 if __name__ == "__main__" :
49     s =StringIO.StringIO(file(sys.argv[1]).read())
50     odt = OpenDocumentTextFile(s)
51     print odt.toString().encode('ascii','replace')
52
53 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: