python open word_使用Python在OpenOffice / Microsoft Word中格式化输出
您可以在PyQt4中使用QTextDocument,QTextCursor和QTextDocumentWriter . 一個(gè)簡單的例子,展示如何寫入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
作為獎(jiǎng)勵(lì),您還可以使用QPrinter打印到pdf文件 .
總結(jié)
以上是生活随笔為你收集整理的python open word_使用Python在OpenOffice / Microsoft Word中格式化输出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python服务端开发调试日志系统_Py
- 下一篇: opencv 通过标定摄像头测量物体大小