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 小时前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学6 小时前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田1 天前
Pydantic校验配置文件
python
hboot1 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi1 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi1 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽1 天前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
用户8358086187911 天前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L2 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅2 天前
海天线算法的前世今生
python·计算机视觉