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

效果如图:

相关推荐
Y1rong17 小时前
C++ QT之记事本
开发语言·qt
吴佳浩20 小时前
大模型量化部署终极指南:让700亿参数的AI跑进你的显卡
人工智能·python·gpu
diegoXie21 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言
程序员小白条21 小时前
0经验如何找实习?
java·开发语言·数据结构·数据库·链表
七牛云行业应用21 小时前
解决OSError: No space left... 给DeepSeek Agent装上无限云硬盘
python·架构设计·七牛云·deepseek·agent开发
liulilittle21 小时前
C++ 浮点数封装。
linux·服务器·开发语言·前端·网络·数据库·c++
BoBoZz1921 小时前
CutWithScalars根据标量利用vtkContourFilter得到等值线
python·vtk·图形渲染·图形处理
失散1321 小时前
Python——1 概述
开发语言·python
萧鼎1 天前
Python 图像哈希库 imagehash——从原理到实践
开发语言·python·哈希算法
qq_251533591 天前
使用 Python 提取 MAC 地址
网络·python·macos