[英]Convert HTML to Word document with python-docx?
I want to generate Word document from HTML field( a field that you can write into a text and set it Bold, Italic, Font color size,..).我想从 HTML 字段(您可以将其写入文本并将其设置为粗体、斜体、字体颜色大小等的字段)生成 Word 文档。 i used for this Python-docx to generate the WOrd document .Everythink is Ok (Adding picture, Text,..)the only problem is the style.我用这个 Python-docx 来生成 WOrd 文档。Everythink 是好的(添加图片,文本,..)唯一的问题是样式。 The problem is i have the content in the word document but without style.问题是我在word文档中有内容但没有样式。
i tried to save the content like a HTML file and after that to create a python-docx file like the following:我尝试将内容保存为 HTML 文件,然后创建如下所示的 python-docx 文件:
html_f=open('f_html.html','w')
html_f.write(u''+contenu)
html_f.close()
doc2=docx.Document('f_html.docx')
But i dont't have a result and Document() haven't find the file.但是我没有结果并且 Document() 没有找到该文件。 Any help please请任何帮助
Python-docx only accepts plain text. Python-docx 只接受纯文本。 You can use pywin32 extensions for windows to convert your html file.您可以使用 Windows 的pywin32扩展来转换您的 html 文件。 A simple example i found:我发现了一个简单的例子:
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()
Alternatively:或者:
from htmldocx import HtmlToDocx
new_parser = HtmlToDocx()
new_parser.parse_html_file("html_filename", "docx_filename")
#Files extensions not needed, but tolerated
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.