3.py 687 B

123456789101112131415161718192021222324252627282930
  1. from docx import Document
  2. from html2docx import html2docx
  3. def convert_html_to_docx(html_content, output_path):
  4. # 将 HTML 内容转换为 DOCX
  5. _io = html2docx(html_content, "1234")
  6. with open(output_path,"wb") as f:
  7. f.write(_io.getvalue())
  8. print(f"DOCX file saved to: {output_path}")
  9. if __name__ == "__main__":
  10. # 示例:将 HTML 内容转换为 DOCX
  11. html_content = """
  12. <div>
  13. <div>
  14. <b>Hello</b>, <i>this is</i> <font color='red'>rich text</font>.
  15. </div>
  16. <div>
  17. <img src="D:/Workspace2016/BIDINLTK/dev/bert/img.png"/>
  18. </div>
  19. </div>
  20. """
  21. output_docx_path = "output_document.docx"
  22. convert_html_to_docx(html_content, output_docx_path)