【问题标题】:How can i convert html to word docx in python?如何在 python 中将 html 转换为 word docx?
【发布时间】:2017-03-14 06:33:13
【问题描述】:
import pypandoc
output = pypandoc.convert_file('file.html', 'docx', outputfile="file1.docx")
assert output == ""

它正在生成新的 docx 文件,但忽略样式。

谁能告诉我如何生成带有样式的新 docx 文件?

提前感谢您的回答。

【问题讨论】:

标签: python html ubuntu-16.04 doc


【解决方案1】:

在 Windows 中,最简单的方法是通过 pywin32 插件使用 MS Word。 Here is good 用示例代码回答。

使用 pypandoc:

output = pypandoc.convert(source='/path/to/file.html', format='html', to='docx', outputfile='/path/to/output.docx', extra_args=['-RTS'])

阅读this 获取额外参数。

【讨论】:

  • 它工作正常,但 pypandoc 不支持 css 样式。
  • @СергейЧабаненко 你可以使用extra_args='--css=custom_file.css' 来包含css文件。您可以使用静态文件或使用模板使用 python 生成临时文件。
【解决方案2】:

你也可以在 python 3.x 中使用htmldocx

from htmldocx import HtmlToDocx

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

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2017-09-23
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多