6

My point is that using either pod (from appy framework, which is a pain to use for me) or the OpenOffice UNO bridge that seems soon to be deprecated, and that requires OOo.org to run while launching my script is not satisfactory at all.

Can anyone point me to a neat way to produce a simple yet clean ODT (tables are my priority) without having to code it myself all over again ?

edit: I'm giving a try to ODFpy that seems to do what I need, more on that later.

4

2 回答 2

7

您使用 odfpy 的里程可能会有所不同。我不喜欢它——我最终使用了一个模板 ODT,在 OpenOffice 中创建,用 ziplib 和 elementtree 打开 contents.xml,然后更新它。(在您的情况下,它只会创建相关的表格行和表格单元节点),然后记录所有内容。

这实际上很简单,但要使 ElementTree 与 XML 命名空间一起正常工作。(记录很差)但是可以做到。我没有例子,对不起。

于 2011-04-02T03:26:23.953 回答
5

要编辑 odt 文件,我的回答可能无济于事,但如果您想创建新的 odt 文件,您可以在PyQt4中使用 QTextDocument、QTextCursor 和 QTextDocumentWriter 。显示如何写入 odt 文件的简单示例:

>>>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 还可以插入表格、框架、块、图像。更多信息。更多信息请访问:http: //qt-project.org/doc/qt-4.8/qtextcursor.html

作为奖励,您还可以使用 QPrinter 打印到 pdf 文件。

于 2012-10-04T14:34:35.947 回答