123456789101112131415161718192021222324252627282930 |
- from docx import Document
- from html2docx import html2docx
- def convert_html_to_docx(html_content, output_path):
- # 将 HTML 内容转换为 DOCX
- _io = html2docx(html_content, "1234")
- with open(output_path,"wb") as f:
- f.write(_io.getvalue())
- print(f"DOCX file saved to: {output_path}")
- if __name__ == "__main__":
- # 示例:将 HTML 内容转换为 DOCX
- html_content = """
- <div>
- <div>
- <b>Hello</b>, <i>this is</i> <font color='red'>rich text</font>.
- </div>
- <div>
- <img src="D:/Workspace2016/BIDINLTK/dev/bert/img.png"/>
- </div>
- </div>
- """
- output_docx_path = "output_document.docx"
- convert_html_to_docx(html_content, output_docx_path)
|