Is there an easy way to write ODT using Python?

My point is that using pod (from the application area, which is a pain for me) or the OpenOffice UNO bridge, which seems to be obsolete soon, and that to run OOo.org while running my script does not is satisfactory.

Can someone point me to a neat way to create a simple but clean ODT (tables are my priority) without having to code it first?

edit: I'm trying to run ODFpy , which seems to do what I need, more on that later.

+6
python pod odt uno
source share
2 answers

Your odfpy mileage may vary. I didn’t like it - I ended up using the ODT template created in OpenOffice, opening the .xml contents with ziplib and elementtree and updating it. (In your case, he would create only the corresponding row tables and table cell nodes), and then write everything back.

It's actually simple, but for ElementTree to work properly with XML namespaces. (this does not document well) But it can be done. I have no example, sorry.

+7
source share

To edit individual files, my answer may not help, but if you want to create new odt files, you can use QTextDocument, QTextCursor and QTextDocumentWriter in PyQt4 . A simple example to show how to write to an odt file:

>>>from pyqt4 import QtGui # Create a document object >>>doc = QtGui.QTextDocument() # Create a cursor pointing to the beginning of the document >>>cursor = QtGui.QTextCursor(doc) # Insert some text >>>cursor.insertText('Hello world') # Create a writer to save the document >>>writer = QtGui.QTextDocumentWriter() >>>writer.supportedDocumentFormats() [PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')] >>>odf_format = writer.supportedDocumentFormats()[1] >>>writer.setFormat(odf_format) >>>writer.setFileName('hello_world.odt') >>>writer.write(doc) # Return True if successful True 

QTextCursor can also insert tables, frames, blocks, images. More information. Additional information: http://qt-project.org/doc/qt-4.8/qtextcursor.html

As a bonus, you can also print the PDF file using QPrinter.

+5
source share

All Articles