python - 更改pdf中文本的字体高亮颜色(fitz模块)

python 复制代码
import fitz

doc = fitz.open(r"e:/test.pdf")
page=doc[0]

# 按照指定的位置设置颜色
highlight = page.add_highlight_annot((20, 500,60, 520))
highlight.set_colors(stroke=[1, 1, 0]) # light red color (r, g, b)  颜色rgb每个除以255得出
highlight.update()

# 按照查找的文本设置颜色
text="2024 年是龙年"
text_instances = page.search_for(text,quads=True)
for inst in text_instances:
    highlight = page.add_highlight_annot(inst)
    highlight.update()

doc.save(r"e:/test.pdf",incremental=True,encryption=0)
# 注意打开文本和保存文本为一个文件

效果如图:

相关推荐
笨笨聊运维6 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite6 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
Want5956 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
小毛驴8507 小时前
软件设计模式-装饰器模式
python·设计模式·装饰器模式
coderxiaohan7 小时前
【C++】多态
开发语言·c++
gfdhy7 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
闲人编程7 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
weixin_457760007 小时前
Python 数据结构
数据结构·windows·python
Eiceblue7 小时前
通过 C# 将 HTML 转换为 RTF 富文本格式
开发语言·c#·html