import fitz # PyMuPDF
# PDFファイルを開く
doc = fitz.open("sample.pdf")
# ハイライトを追加するページと座標を指定
page_number = 0 # ページ番号 (0始まり)
highlight_rects = [
(100, 200, 300, 250), # (x0, y0, x1, y1) の座標 (左上と右下)
(150, 300, 350, 350) # 複数領域も指定可能
]
# 指定ページを取得
page = doc.load_page(page_number)
# 指定座標に塗りつぶしハイライトを追加
for rect in highlight_rects:
# 矩形領域を定義
annotation = page.add_rect_annot(fitz.Rect(rect))
# 塗りつぶし色 (黄色) と透明度を設定
annotation.set_colors(fill=(1, 1, 0)) # RGB: 黄色
annotation.set_opacity(0.3) # 透明度30%
annotation.update()
# 保存
doc.save("sample_output.pdf")
doc.close()