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
相关推荐
高斯的手稿080115 小时前
Django里面,多个APP的url设置,每个APP单独对应HTML设置
数据库·django·html
摸鱼仙人~16 小时前
如何对量化前后的模型进行评估
python
overmind16 小时前
oeasy玩py111列表_排序_sort_比较大小
python
JH灰色16 小时前
【大模型】-modelscope魔搭
python
STLearner16 小时前
AAAI 2026 | 时空数据(Spatial-temporal)论文总结[上](时空预测,轨迹挖掘,自动驾驶等)
大数据·人工智能·python·深度学习·机器学习·数据挖掘·自动驾驶
知行合一。。。16 小时前
Python--02--流程控制语句
开发语言·python
码农小卡拉16 小时前
Java多线程:CompletableFuture使用详解(超详细)
java·开发语言·spring boot·python·spring·spring cloud
Robot侠17 小时前
从 Python 到 Ollama:将微调后的 Llama-3/Qwen 一键导出为 GGUF
开发语言·python·llama·qwen
l1t17 小时前
Python 字符串反转方法
linux·开发语言·python
Eiceblue17 小时前
使用 Python 写入多类型数据至 Excel 文件
开发语言·python·excel