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

效果如图:

相关推荐
weixin_447195295 小时前
【无标题】
pytorch·python
迅猛龙办公室5 小时前
Python实现简单的人名对话
python
阿坨6 小时前
firestart:一行命令启动你的日常应用和网页
python·pypi·cli·click
老歌老听老掉牙6 小时前
麻花钻切屑形态演变的力学机制与临界条件分析
python·算法·钻头
周杰偷奶茶6 小时前
【Java】数组的定义和使用(附管理系统实战案例)
java·开发语言
happylifetree6 小时前
Python18(补充):练习
python
橙子圆1236 小时前
JUC之集合类不安全
java·开发语言
阿里嘎多学长7 小时前
2026-09-29 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Bruce_Liuxiaowei7 小时前
网页版 DeepSeek “harness desktop“ WebGL 上下文创建失败排查实录
开发语言·javascript·webgl
weixin_416667967 小时前
银河麒麟V10看门狗试验-1
网络·chrome·python