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
相关推荐
金銀銅鐵5 分钟前
[Python] 借助图形化界面探索模n运算的规律
python·数学
Hesionberger6 分钟前
快速求解完全平方数的最少数量
开发语言·数据结构·python·算法·leetcode·c#
xywww1684 小时前
大模型 API 选型实战:GPT、Gemini、Claude 接入时该看哪些指标?
运维·服务器·人工智能·python·gpt·langchain
夜雪一千9 小时前
Python enumerate() 函数完整详解:遍历同时获取索引,告别手动计数
服务器·windows·python
能有时光10 小时前
PyTorch KernelAgent 源码解读 ---(4)--- ExtractorAgent
人工智能·pytorch·python
_Jimmy_10 小时前
Python 协程库如何使用以及有哪些使用场景
python
IMPYLH10 小时前
HTML 的 <data> 元素
前端·html
aqi0010 小时前
15天学会AI应用开发(十七)使用LangGraph实现会话记忆功能
人工智能·python·大模型·ai编程·ai应用
第一程序员11 小时前
Rust Agent 子进程执行:Command 之前,先定义输入和超时
python·rust·github
skywalk816311 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程