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
相关推荐
Muyuan19987 小时前
27.RAG 系统中的上下文充分性判断:从 Chunk 数量、FAISS 距离到 LLM Relevance Gate
python·django·pdf·fastapi·faiss
U盘失踪了8 小时前
python curl转python脚本
开发语言·chrome·python
FQNmxDG4S8 小时前
Java泛型编程:类型擦除与泛型方法的应用场景
java·开发语言·python
bzmK1DTbd8 小时前
JDBC编程规范:PreparedStatement与事务管理
数据库·python·eclipse
我星期八休息8 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表
代码小书生8 小时前
math,一个基础的 Python 库!
人工智能·python·算法
leo825...9 小时前
Claude Code Skills 清单(本地)
java·python·ai编程
其实防守也摸鱼9 小时前
CTF密码学综合教学指南--第三章
开发语言·网络·python·安全·网络安全·密码学
A7bert7779 小时前
【YOLOv8pose部署至RDK X5】模型训练→转换bin→Sunrise 5部署
c++·python·深度学习·yolo·目标检测
gqk0110 小时前
【无标题】
python