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

效果如图:

相关推荐
geovindu17 分钟前
go: Iterative Algorithms
开发语言·后端·算法·golang·迭代算法
学逆向的18 分钟前
汇编——JCC指令
开发语言·汇编·网络安全
mayaairi1 小时前
JS循环语句深度解析:嵌套for、while与do...while
开发语言·前端·javascript
郭老二2 小时前
【Python】基本语法:装饰器语法糖@
python
kite01212 小时前
Go语言Map深度解析与最佳实践
开发语言·后端·golang
_Jimmy_3 小时前
Agent常用检索器的详细介绍
python·langchain
小柯南敲键盘3 小时前
图片翻译API接入与自动化实现指南
运维·python·自动化
蓝创工坊Blue Foundry3 小时前
个人藏书太多怎么整理?用 OCR 字段提取汇总成电子书目
pdf·ocr·excel·文心一言·paddlepaddle·paddle
l156469483 小时前
突围!图文混合知识库难以解析,Kimi-K3 原生视觉架构深耕知识工作,DMXAPI 统一接口,缩短项目开发周期
java·大数据·开发语言
旅僧3 小时前
Q-learning(自用)
python·机器学习