pdf-editing
БесплатноБез исполняемых скриптовНе проверенComplete guide for reading and editing PDF documents with PyMuPDF.
от benchflow-ai★ 1 941 звезда на GitHubОб этом скилле
PDF Editing Skill
CRITICAL RULES - READ FIRST
NEVER DO THESE:
- NEVER use strikethrough lines to cross out text
- NEVER rasterize or flatten the PDF to images
- NEVER convert PDF pages to PNG/JPG and draw on them
- NEVER use pdf-to-image-to-pdf workflows
- NEVER use add_redact_annot() with BLACK fill (use WHITE fill instead)
- NEVER add text NEXT TO old values - REPLACE them at the SAME position
TWO APPROACHES - CHOOSE THE RIGHT ONE:
For REPLACING text (e.g., updating name, email, DOB):
- Use
draw_rect()with white fill to cover old text - Use
insert_text()at the SAME position - Text layer is preserved
- Use
For TRUE REDACTION of sensitive data (e.g., student ID):
- Use
add_redact_annot(rect, fill=(1,1,1))with WHITE fill - Call
apply_redactions()to REMOVE text from PDF structure - Then
insert_text()to add masked value (e.g., "****5678") - Original text is completely removed, not just covered
- Use
Overview
USE PYTHON WITH PyMuPDF (fitz) - it is pre-installed and produces the best results.
PyMuPDF preserves the text layer properly, making text extractable after editing. JavaScript libraries like pdf-lib may create text that tools like pypdf cannot extract.
# PyMuPDF is already installed - just use it
python3 -c "import fitz; print('PyMuPDF ready')"
Reading PDF Content
import fitz
doc = fitz.open("input.pdf")
page = doc[0]
# Extract all text to understand the document
text = page.get_text()
print(text)
Finding Text Positions
# search_for() returns list of rectangles where text is found
rects = page.search_for("Label Text")
if rects:
rect = rects[0]
# rect.x0, rect.y0 = top-left corner
# rect.x1, rect.y1 = bottom-right corner
print(f"Found at: ({rect.x0}, {rect.y0}) to ({rect.x1}, {rect.y1})")
Inserting Text
# Insert text at a specific position
page.insert_text(
(x_position, y_position), # coordinates
"text to insert",
fontsize=11,
color=(0, 0, 0) # black
)
doc.save("output.pdf")
Common Pattern: Fill Empty Form Fields
When a form field is empty (no existing value), insert text next to the label:
import fitz
doc = fitz.open("input.pdf")
page = doc[0]
# Find label, insert value to the right of it
label_rect = page.search_for("FIELD LABEL:")[0]
page.insert_text((label_rect.x1 + 5, label_rect.y1), "value", fontsize=11)
# For today's date
from datetime import datetime
date_rect = page.search_for("Date")[0]
today = datetime.now().strftime("%Y/%m/%d")
page.insert_text((date_rect.x1 + 5, date_rect.y1), today, fontsize=11)
# For signatures - insert the person's name
sig_rect = page.search_for("signature")[0]
page.insert_text((sig_rect.x1 + 5, sig_rect.y1), "Full Name", fontsize=12)
doc.save("output.pdf")
Replacing Existing Text (CRITICAL - MUST FOLLOW)
When you need to replace text that's already in the PDF with new/correct values:
- First extract the PDF text to see what's currently there
- Compare with the correct values from your input source
- For any value that needs to change: COVER with white rectangle, then insert new text AT THE SAME POSITION
WRONG vs RIGHT approaches:
WRONG - DO NOT DO THIS:
# WRONG: Adding text next to old value
page.insert_text((old_rect.x1 + 10, old_rect.y1), new_value) # NO!
# WRONG: Using strikethrough
page.draw_line(start, end, color=(0,0,0)) # NO!
# WRONG: Rasterizing to image
pix = page.get_pixmap() # NO! Destroys text layer
RIGHT - DO THIS:
import fitz
doc = fitz.open("input.pdf")
page = doc[0]
# First, extract text to see what's in the PDF
current_text = page.get_text()
print(current_text) # Examine what values exist
# To replace a value you found that needs changing:
old_value = "..." # The value you found in the PDF that's wrong
new_value = "..." # The correct value from your input source
rects = page.search_for(old_value)
if rects:
rect = rects[0]
# STEP 1: Draw WHITE rectangle to COMPLETELY COVER old text
page.draw_rect(rect, color=(1, 1, 1), fill=(1, 1, 1), width=0)
# STEP 2: Insert new text at the SAME position (not offset!)
page.insert_text((rect.x0, rect.y1), new_value, fontsize=11, color=(0, 0, 0))
doc.save("output.pdf")
Key points:
draw_rect(rect, fill=(1,1,1), width=0)draws a WHITE filled rectangle(1, 1, 1)is white in RGB (0-1 scale)- Insert new text at
rect.x0(same X position), NOTrect.x1 + offset - The old text becomes invisible under the white rectangle
- The new text appears in the same location
- Text layer is preserved (text remains extractable)
TRUE REDACTION for Sensitive Data (IMPORTANT!)
For sensitive data like student IDs, you must use TRUE REDACTION that removes the original text from the PDF structure. A white rectangle only VISUALLY covers text - tools like pypdf can still extract the hidden text!
CRITICAL DISTINCTION:
draw_rect()= Visual
Установить pdf-editing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
FAQ
Что делает скилл pdf-editing?
Complete guide for reading and editing PDF documents with PyMuPDF.
Как установить скилл pdf-editing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл pdf-editing запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
docx
Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include…
от AnthropicUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple…
от Anthropicxlsx
Use this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing…
от Anthropicpptx
Use this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or…
от Anthropic