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

效果如图:

相关推荐
仙女修炼史9 分钟前
gradcam在yolo中的应用
人工智能·python·yolo
离陌在学C#21 分钟前
C# 异步编程:从 async/await 到 Task 实战指南
开发语言·数据库·c#
羚尔29 分钟前
C语言数组
c语言·开发语言
李可以量化1 小时前
Redis Client 从了解到精通(六):redis-py 服务控制与状态监控辅助函数详解
python
女神下凡1 小时前
芯参谋(11): 各种存储芯片电路设计
开发语言·嵌入式硬件
李可以量化1 小时前
Redis Client 从了解到精通(六)下:redis-py 时间、库管理与持久化辅助函数详解
python
晓窗科技1 小时前
AI基座哪家好
大数据·人工智能·python
冬夜戏雪1 小时前
笔试的Scanner in Scanner out
java·开发语言
麻雀飞吧1 小时前
学量化:看到“近期工具推荐”时,先问工具要解决什么问题
人工智能·python
Zane1994121 小时前
HashSet、TreeSet、LinkedHashSet:底层都是“套壳“
java·开发语言