Python 中的 html 到 .doc 转换器?

发布于 2024-10-04 02:06:08 字数 101 浏览 0 评论 0 原文

我正在使用 pisa,它是一个用于 Python 的 HTML 到 PDF 转换库。

Word 文档是否存在同样的东西:Python 的 HTML 到 .doc 转换库?

I am using pisa, which is an HTML to PDF conversion library for Python.

Does there exist the same thing for a Word document: an HTML to .doc conversion library for Python?

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

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

发布评论

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

评论(4

世俗缘 2024-10-11 02:06:08

您可以使用 Windows 的 pywin32 python 扩展中的 win32com,让 MS Word 为您转换它。一个简单的例子:

import win32com.client

word = win32com.client.Dispatch('Word.Application')

doc = word.Documents.Add('example.html')
doc.SaveAs('example.doc', FileFormat=0)
doc.Close()

word.Quit()

You could use win32com from the pywin32 python extensions for windows, to let MS Word convert it for you. A simple example:

import win32com.client

word = win32com.client.Dispatch('Word.Application')

doc = word.Documents.Add('example.html')
doc.SaveAs('example.doc', FileFormat=0)
doc.Close()

word.Quit()
德意的啸 2024-10-11 02:06:08

虽然我不知道有一个直接模块可以让您进行转换,但是:

  1. 您可以首先使用 HTML 转换为纯文本 //www.aaronsw.com/2002/html2text/" rel="noreferrer">html2text 模块。
  2. 之后,您可以使用 python-docx 模块将文本转换为 docdocx 文件。

Though I am not aware of a direct module that can allow you to convert this, however:

  1. You can convert HTML to plain text first using the html2text module.
  2. After that, you can use this the python-docx module to convert the text to a doc or a docx file.
疏忽 2024-10-11 02:06:08

如果其他人在这里尝试以相反的方式进行转换,上面的代码可以工作,但您需要修改 FileFormat 值。

http://msdn.microsoft.com/en-us/library/ff839952.aspx

示例:过滤后的 html 为 10,而不是 0。

In case anybody else lands here attempting to convert the other way around, the above code works, but you need to modify the FileFormat value.

http://msdn.microsoft.com/en-us/library/ff839952.aspx

Example: Filtered html is 10, instead of 0.

梦境 2024-10-11 02:06:08

使用 python3.x 更新修复此问题:

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated

Update with a python3.x fix this:

from htmldocx import HtmlToDocx

new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文