python实现word转html

目录

使用mammoth库

使用spire.doc库


使用mammoth库

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

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文件处理库,不太好的就是商业库转换出的文件有水印。

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值