如何在python中将html转换为word docx?

Jay*_*eru 2 html python doc ubuntu-16.04

import pypandoc
output = pypandoc.convert_file('file.html', 'docx', outputfile="file1.docx")
assert output == ""
Run Code Online (Sandbox Code Playgroud)

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

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

预先感谢您的回答。

Syn*_*ase 11

您还可以在 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
Run Code Online (Sandbox Code Playgroud)


Emi*_*ada 5

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

使用pypandoc:

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

阅读此内容以获取extra_args。