如何使用 Python 创建 Word 文档?

发布于 2024-07-26 02:11:26 字数 1815 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

許願樹丅啲祈禱 2024-08-02 02:11:26

使用 Python 创建 Word 文档的方法有多种:

  • 使用 COM 自动化,使用 MS Word 对象模型(使用 pywin32)创建文档。 http://python.net/crew/pirx/spam7/
  • 使用 Python 自动化 OpenOffice:< a href="http://wiki.services.openoffice.org/wiki/Python" rel="noreferrer">http://wiki.services.openoffice.org/wiki/Python
  • 如果 rtf 格式没问题,使用 PyRTF 库: http://pyrtf.sourceforge.net/

编辑:

由于 COM 已超出问题,我建议如下(受到 @kcrumley 的回答的启发):

使用 UNO 库从 python 自动化 Open Office,在 OOWriter 中打开 HTML 文件,然后另存为 .doc。

EDIT2:

现在有一个纯Python python-docx 项目 看起来不错(我有没用过)。

A couple ways you can create Word documents using Python:

EDIT:

Since COM is out of the question, I suggest the following (inspired by @kcrumley's answer):

Using the UNO library to automate Open Office from python, open the HTML file in OOWriter, then save as .doc.

EDIT2:

There is now a pure Python python-docx project that looks nice (I have not used it).

淡墨 2024-08-02 02:11:26

我尝试了 python-docx 并取得了成功,它使您能够在 Python 中制作和编辑 docx

I tried python-docx with succes, it enables you to make and edit docx within Python

著墨染雨君画夕 2024-08-02 02:11:26

1) 如果您想在当前流程的末尾再进行一步,现在有多种选项可用于将 PDF 文件转换为 Word 文件。 我还没有尝试过 123PDFConverter ,但 CNET 编辑推荐它(相同的链接); 它有免费试用版; 并且它支持自动化。 与任何第 3 方文件转换器一样,您的效果可能会有所不同,具体取决于您的 PDF 的复杂程度以及软件的实际性能。

2) 基于codeape的COM自动化建议,如果您COM自动化Word,您可以在Word中打开实际的HTML文件,并调用“另存为”命令,将其另存为DOC文件。

1) If you want to just stick another step on the end of your current pipeline, there are several options out there now for converting PDF files to Word files. I haven't tried 123PDFConverter, but the CNET Editors recommend it (same link); it has a free trial; and it supports automation. As with any 3rd-party file converter, your mileage may vary, depending how complicated your PDFs are, and how good the software actually is.

2) Building on codeape's COM automation suggestion, if you COM automate Word, you can open your actual HTML file in Word, and call the "Save As" command, to save it as a DOC file.

金橙橙 2024-08-02 02:11:26

我也不得不用 python 做类似的事情。 这是比我想要的更多的手动工作,但使用 pyRTF 创建的文档导致 Word 和 OpenOffice 崩溃,我没有动力去尝试解决这个问题。

我发现创建具有我想要的样式的 Word 文档模板是最简单的(但并不理想)。 然后我的 Python 创建一个 HTML 文件,其

样式标记在 Word 样式之后。 然后我在 Word 中打开 HTML 文件并在 Word 中打开模板。 我将 HTML 文件中的所有文本剪切并粘贴到模板中,Word 会根据我之前设置的样式重新格式化所有文本。 这适用于我的情况下的偶尔文件。 它可能不适合您的情况。 供参考。

I have had to do something similar with python as well. It is far more manual work than I want, but documents created with pyRTF were causing Word and OpenOffice to crash and I didn't have the motivation to try to figure it out.

I have found it simplest (but not ideal) to create a Word document template with the styles I want. Then my Python creates an HTML file whose <p> styles are labeled after the Word styles. Then I open the HTML file in Word and open the template in Word. I cut and paste all text from the HTML file into the template, and Word re-formats it all according to the styles I had set up previously. That works for the occasional file in my situation. It might not work for your situation. FYI.

一腔孤↑勇 2024-08-02 02:11:26

您可以将其编写为 WordML XML 文件并将其压缩为 .docx 格式吗? 如果您的客户尚未使用 Office 2007,那么他们所需要的只是 Word 2007 筛选器。

那里有很多例子。

从 2003 年开始,您还可以将 XML 直接加载到 Word 中,或者 所以有人告诉我

Can you write is as the WordML XML files and zip it up into the .docx format? All your client would need is the Word 2007 filter if they aren't on Office 2007 already.

There are many examples out there.

You can also load XML directly into Word, starting with 2003, or so I've been told.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文