Python把word转为html(去水印版)

背景:

有一份word文档,但是通过 aspose.words 转化为图片后会有水印,虽然上下方的水印可以通过截取去掉,但是文本中间的logo水印无法去除,所以需要转为html进行去除

版本说明:aspose-words==24.2.0

输入:一个word的文件路径

输出:一个同名的html问价

复制代码
import aspose.words.saving as saving
import aspose.words as aw
from bs4 import BeautifulSoup
import re


def word_html(file_name):
    docx = aw.Document(file_name)
    save_options = saving.HtmlSaveOptions(aw.SaveFormat.HTML)
    save_options.export_images_as_base64 = True

    docx.save('tmp.html', save_options)

    html_content = open('tmp.html', "r", encoding="utf-8")
    soup = BeautifulSoup(html_content, features="lxml")
    # 删除指定的aspose的内容
    for tag in soup.find_all(style=re.compile("-aw-headerfooter-type:")):
        tag.extract()
    word_key_tag = soup.find("p", text=re.compile("Evaluation Only"))
    word_key_tag.extract()

    f = open(file_name.split('.')[0] + '.html', "w", encoding="utf-8")
    f.write(soup.prettify())
    f.close()

if __name__ == '__main__':
    
    word_html(file_name='1.docx')
    # 输出 1.html
相关推荐
zone77391 小时前
001:简单 RAG 入门
后端·python·面试
F_Quant2 小时前
🚀 Python打包踩坑指南:彻底解决 Nuitka --onefile 配置文件丢失与重启报错问题
python·操作系统
允许部分打工人先富起来3 小时前
在node项目中执行python脚本
前端·python·node.js
IVEN_3 小时前
Python OpenCV: RGB三色识别的最佳工程实践
python·opencv
haosend3 小时前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽4 小时前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_21 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang1 天前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮1 天前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling1 天前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python