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)
相关推荐
用户548178459095010 小时前
Java 与 Python 在大模型与智能体领域的选型
java·python
yiangL_10 小时前
1、Python 输入输出实验|print 与 input 人机交互实践
开发语言·python·人机交互
高洁0111 小时前
世界模型的因果内核:从预测表象到理解机制
人工智能·python·深度学习·transformer·迁移学习
数字融合11 小时前
透明化视频动态三维重建空间数据零核心技术
人工智能·python·virtualenv
AI创界者12 小时前
Python 进阶实战(五十四章):大模型推理网关终极架构——分布式全局前缀 Trie 缓存同步与多网关高可用协同调度
人工智能·python·aigc
萧子琦12 小时前
零第三方依赖能做到什么程度?开源两个纯标准库安全小工具:StarShield Forensics 与澄镜 ClearLens
python
用户912940468484212 小时前
Codex 从零到能交付(19):让 Codex 做代码审查:发现问题后怎样验证
python
小白勇闯网安圈13 小时前
day_2
python
叮当小飞鱼13 小时前
RAG幻觉
python
赵大仁13 小时前
Agent 工具调用可靠性:超时、重试、幂等与人工确认怎么设计
python·agent·幂等·工具调用·人工确认