python实现word转html

目录

使用mammoth库

使用spire.doc库


使用mammoth库

mammoth库支持将word转为HTML和markdown格式的文件。

python 复制代码
import mammoth

def word_html(word_file):
    html_save_name = fr'{word_file.split('.')[0]}.html'
    with open(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库

强大的word文件处理库,不太好的就是商业库转换出的文件有水印。

python 复制代码
from spire.doc import Document, FileFormat


def word_html(word_file):
    html_save_name = fr'{word_file.split('.')[0]}.html'
    doc = Document()
    doc.LoadFromFile(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)
相关推荐
代码山河5 小时前
Python就业方向全解析:后端、爬虫、数据分析、AI你选哪个?
python·学习·架构·教程·面向对象·项目
在世修行5 小时前
智学文枢:题目流水线
python·多线程·状态机·pyside
梯度下降者6 小时前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
“AI国潮设计-小江”7 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
benchmark_cc8 小时前
用 Python 拉 A 股全市场实时快照:Notebook 请求、结果校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
伞伞悦读8 小时前
【第70期】Adam二阶矩不是方差:偏差校正、动量与AdamW完整实验
python
外收内放9 小时前
Python基础语法练习题(73)
java·前端·python
锋行天下9 小时前
Langgraph HITL 中断运行,用户输入后输出打字效果
人工智能·python
时早玖沐10 小时前
给 AstrBot 写一个天气推送插件:12 次提交、6 个坑
python·开源·ai编程
触底反弹10 小时前
🚀 FastAPI 五大重难点深度剖析:从 500 报错到彻底搞懂 Depends、Pydantic 与 SQLAlchemy
python·fastapi