htmledit_views">
目录
使用mammoth库
使用spire.doc库
使用mammoth库
mammoth库支持将html" title=word>word转为HTML和markdown格式的文件。
html" title=python>python">import mammothdef html" title=word>word_html(html" title=word>word_file):html_save_name = fr'{html" title=word>word_file.split('.')[0]}.html'with open(html" title=word>word_file, 'rb') as f:data = mammoth.convert_to_html(f)with open(html_save_name, 'w') as f:f.write(data.value)
使用spire.doc库
强大的html" title=word>word文件处理库,不太好的就是商业库转换出的文件有水印。
html" title=python>python">from spire.doc import Document, FileFormatdef html" title=word>word_html(html" title=word>word_file):html_save_name = fr'{html" title=word>word_file.split('.')[0]}.html'doc = Document()doc.LoadFromFile(html" title=word>word_file)doc.SaveToFile(html_save_name, FileFormat.Html)doc.Close()with open(html_save_name, 'r', encoding='utf-8') as f:data = f.read().replace('Evaluation Warning: The document was created with Spire.Doc for Python.', '') # 去掉商业库spire.doc生成的水印信息with open(html_save_name, 'w', encoding='utf-8') as f:f.write(data)