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 = """
Hello, this is rich text.
""" output_docx_path = "output_document.docx" convert_html_to_docx(html_content, output_docx_path)