使用 Python 在 OpenOffice/Microsoft Word 中格式化输出
我正在开发一个需要格式化、可编辑输出的项目(Python)。 由于最终用户不会精通技术,因此输出需要采用文字处理器可编辑的格式。 格式很复杂(要点、段落、粗体等)。
有没有办法使用Python生成这样的报告? 我觉得应该有一种方法可以使用 Microsoft Word/OpenOffice 模板和 Python 来做到这一点,但我找不到任何足够先进的东西来获得良好的格式。 有什么建议么?
I am working on a project (in Python) that needs formatted, editable output. Since the end-user isn't going to be technically proficient, the output needs to be in a word processor editable format. The formatting is complex (bullet points, paragraphs, bold face, etc).
Is there a way to generate such a report using Python? I feel like there should be a way to do this using Microsoft Word/OpenOffice templates and Python, but I can't find anything advanced enough to get good formatting. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
一个鲜为人知且有点邪恶的事实:如果您创建一个 HTML 文件,并在其上添加 .doc 扩展名,Word 会将其作为 Word 文档打开,而大多数用户对此一无所知。
除非一个非常技术的人会说,我的这是一个小Word文件! :)
A little known, and slightly evil fact: If you create an HTML file, and stick a .doc extension on it, Word will open it as a Word document, and most users will be none the wiser.
Except maybe a very technical person will say, my this is a small Word file! :)
为此使用 Python Docx 模块 - 100% Python、表格、图像、文档属性、标题、段落等等。
Use the Python Docx module for this - 100% Python, tables, images, document properties, headings, paragraphs, and more.
" 格式很复杂(要点、段落、粗体等),"
使用 RST。
由于它是纯文本,因此生成起来很简单。
编辑起来很简单,因为它是纯文本,带有一些额外的字符来提供结构信息。
它使用一堆工具很好地格式化。
" The formatting is complex(bullet points, paragraphs, bold face, etc), "
Use RST.
It's trivial to produce, since it's plain text.
It's trivial to edit, since it's plain text with a few extra characters to provide structural information.
It formats nicely using a bunch of tools.
我知道有一个odtwriter for docutils。 您可以将输出生成为 reStructuredText 并将其提供给 odtwriter 或查看 odtwriter 正在使用什么后端生成 ODT 并使用它。
(我可能会生成第一个输出,然后破解 odtwriter 来输出我想要的东西(并将修复贡献回项目),因为这可能比尝试直接将你的东西渲染到 ODT 容易得多。)
I know there is an odtwriter for docutils. You could generate your output as reStructuredText and feed it to odtwriter or look into what odtwriter is using on the backend to generate the ODT and use that.
(I'd probably go with generating rst output and then hacking odtwriter to output the stuff I want (and contribute the fixes back to the project), because that's probably a whole lot easier that trying to render your stuff to ODT directly.)
我已经使用 xlwt 使用 python 创建 Excel 文档,但我还不需要编写 word 文件。 我找到了这个包,OOoPy,但我还没有使用它。
此外,您可能想尝试输出 html 文件并让用户在 Word 中打开它们。
I've used xlwt to create Excel documents using python, but I haven't needed to write word files yet. I've found this package, OOoPy, but I haven't used it.
Also you might want to try outputting html files and having the users open them in Word.
您可以在 PyQt4 中使用 QTextDocument、QTextCursor 和 QTextDocumentWriter。 一个简单的例子展示如何写入odt文件:
QTextCursor还可以插入表格、框架、块、图像。 更多信息请访问:
http://qt-project.org/doc/qt-4.8/qtextcursor。 html
作为奖励,您还可以使用 QPrinter 打印为 pdf 文件。
You can use QTextDocument, QTextCursor and QTextDocumentWriter in PyQt4. A simple example to show how to write to an odt file:
QTextCursor also can insert tables, frames, blocks, images. More information at:
http://qt-project.org/doc/qt-4.8/qtextcursor.html
As a bonus, you also can print to a pdf file by using QPrinter.
我认为 OpenOffice 有一些 Python 绑定 - 您应该能够在 Python 中编写 OO 宏。
但我会使用 HTML - Word 和 OO.org 非常擅长编辑它,并且您可以轻松地从 Python 编写它(尽管 Word 节省了很多混乱,这可能会使您的 Python 应用程序解析它变得复杂)。
I think OpenOffice has some Python bindings - you should be able to write OO macros in Python.
But I would use HTML instead - Word and OO.org are rather good at editing it and you can write it from Python easily (although Word saves a lot of mess which could complicate parsing it by your Python app).