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)
# 注意打开文本和保存文本为一个文件

效果如图:

相关推荐
巧克力男孩dd5 分钟前
Python超典型练习题(第一次作业)
开发语言·python·算法
TlSfoward12 分钟前
TLSFOWARD TLS指纹
开发语言·数据库·爬虫·搜索引擎·https·php
闲猫1 小时前
LangChain / Core components / Models
开发语言·python·langchain
-银雾鸢尾-2 小时前
C#中的索引器
开发语言·c#
Qlittleboy3 小时前
PHP的接口参数传递的过多,max_input_vars = 5000
开发语言·php
Aurorar0rua3 小时前
CS50 x 2024 Notes Algorithms - 02
c语言·开发语言·学习方法
en.en..3 小时前
冒泡排序与选择排序完整对比解析
开发语言·数据结构·算法·c#·排序算法
兰令水3 小时前
hot100【acm版】【2026.7.18打卡-java版本】
java·开发语言·算法
cui_ruicheng3 小时前
Python从入门到实战(十三):模块、包与环境管理
开发语言·python
就叫飞六吧3 小时前
子页面和dialog案例
开发语言·javascript·ecmascript