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

效果如图:

相关推荐
南风微微吹20 分钟前
【管综】考研199管理类联考真题试卷及答案解析PDF电子版(2009-2025年)
考研·pdf
todoitbo31 分钟前
Rust新手第一课:Mac环境搭建踩坑记录
开发语言·macos·rust
laplace01231 小时前
PyQt5 + Qt Designer配置指令
开发语言·qt
nvd111 小时前
Python 迭代器 (Iterator) vs. 生成器 (Generator)
开发语言·python
HalvmånEver1 小时前
Linux:基础开发工具(三)
linux·运维·服务器·开发语言·学习·gcc/g++
后端小张1 小时前
【JAVA 进阶】Spring Boot 注解体系与工程实践
java·开发语言·spring boot·后端·spring·spring cloud·java-ee
倔强的石头1061 小时前
Rust实战:使用Axum和SQLx构建高性能RESTful API
开发语言·rust·restful
q***46521 小时前
对基因列表中批量的基因进行GO和KEGG注释
开发语言·数据库·golang
柠石榴1 小时前
GO-1 模型本地部署完整教程
开发语言·后端·golang