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
相关推荐
Zhansiqi42 分钟前
dayy43
pytorch·python·深度学习
紫丁香1 小时前
pytest_自动化测试3
开发语言·python·功能测试·单元测试·集成测试·pytest
杰杰7981 小时前
Python面向对象——类的魔法方法
开发语言·python
chushiyunen2 小时前
python中的魔术方法(双下划线)
前端·javascript·python
深蓝轨迹2 小时前
@Autowired与@Resource:Spring依赖注入注解核心差异剖析
java·python·spring·注解
人工智能AI技术2 小时前
Python 3.14.3更新!内存优化与安全补丁实战应用
python
2401_891655812 小时前
此电脑网络位置异常的AD域排错指南的技术文章大纲
开发语言·python·算法
不要秃头的小孩2 小时前
50. 随机数排序
数据结构·python·算法
qq_417695052 小时前
实战:用OpenCV和Python进行人脸识别
jvm·数据库·python
1941s3 小时前
Google Agent Development Kit (ADK) 指南 第五章:工具集成与自定义
人工智能·python·langchain·agent·adk