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
相关推荐
明月_清风4 小时前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风4 小时前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python
Flittly19 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling1 天前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
databook1 天前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风1 天前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风1 天前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei2 天前
python 抽象基类
python
用户8356290780512 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试