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

效果如图:

相关推荐
jimy18 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
L@ncor8 小时前
第二章可能出现的问题
人工智能·python
y = xⁿ8 小时前
大模型基础概念
python
ctlover8 小时前
排序与查找算法详解
开发语言·python
用户8356290780518 小时前
如何使用 Python 给 Word 文档添加水印
后端·python
YYYing.9 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
泡干脆面就番茄9 小时前
深度学习:PyTorch框架初识——MNIST手写数字识别
python·深度学习
shirsl9 小时前
算法 Day 5 树 / 二叉树 + DFS
数据结构·python·算法
门思科技9 小时前
开源网关有哪些:主流类型梳理
网络·python·物联网
szephyr9 小时前
Python 爬虫合规与反爬实战:从 requests 到 Playwright
爬虫·python·requests·playwright·反爬