agentleFS
abduznikabduznik/abduznik-skills/skills/pdf-surgical-edits/SKILL.md

pdf-surgical-edits

Skill2 starsChanged 2 months agoMITView on GitHub

Redact, draw, and add text to existing PDFs via PyMuPDF.

  • Installs packages

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use page.get_drawings() to list all vector shapes — useful for identifying which drawings belong to which table area:

    line 119
  2. Very specific:

    Hebrew text reversed without bidi: Always use get_display() from python-bidi for RTL scripts.

    line 132

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

PDF Surgical Edits

Modify existing PDF pages in-place: blank regions, redraw shapes, insert text. Uses PyMuPDF (pymupdf).

Prerequisites

python -m pip install PyMuPDF
# For RTL/Hebrew/Arabic text:
python -m pip install python-bidi

Workflow: Redact → Draw → Insert

1. Redact (blank an area)

Removes all drawings AND text in the rect. Fills with a color (usually white).

import pymupdf
doc = pymupdf.open('input.pdf')
page = doc[0]
page.add_redact_annot(pymupdf.Rect(x0, y0, x1, y1), fill=(1, 1, 1))
page.apply_redactions()

Redaction removes everything in the rect — vector shapes, text, images. Grid lines inside the rect are also removed. Redraw them after if needed.

2. Draw shapes (rebuild grid, add colored blocks)

def draw_rect(page, x0, y0, x1, y1, fill=None, color=(0,0,0), width=0.5):
    shape = page.new_shape()
    shape.draw_rect(pymupdf.Rect(x0, y0, x1, y1))
    if fill:
        shape.finish(fill=fill, color=fill, width=width)
    else:
        shape.finish(color=color, width=width)
    shape.commit()

3. Insert text

LTR text (English, numbers, codes)

page.insert_text(
    pymupdf.Point(x, y), 'Hello 123',
    fontfile='C:/Windows/Fonts/tahoma.ttf', fontname='tah', fontsize=6
)

RTL text (Hebrew, Arabic) — REQUIRES python-bidi

pymupdf does NOT apply Unicode bidi. Hebrew text inserted in logical order renders backwards in PDF viewers. Convert to visual order first:

from bidi.algorithm import get_display

visual = get_display('תרגול-1 אלגוריתמים')
page.insert_text(
    pymupdf.Point(x, y), visual,  # visual order, NOT logical
    fontfile='C:/Windows/Fonts/tahoma.ttf', fontname='tah', fontsize=6
)

Right-align Hebrew text — calculate x from right edge:

def rtl(page, x_right, y, text, fontsize=6, char_width=0.38):
    visual = get_display(text)
    w = len(text) * fontsize * char_width
    page.insert_text(
        pymupdf.Point(x_right - w, y), visual,
        fontfile='C:/Windows/Fonts/tahoma.ttf', fontname='tah', fontsize=fontsize
    )

char_width of 0.38 works for Tahoma at small sizes. Verify with a test render for other fonts.

PDF Table Structure Analysis

Map table columns/rows from text coordinates:

blocks = page.get_text('dict')['blocks']
for b in blocks:
    if 'lines' not in b:
        continue
    for line in b['lines']:
        for span in line['spans']:
            text = span['text'].strip()
            bbox = span['bbox']  # (x0, y0, x1, y1)
            cx = (bbox[0] + bbox[2]) / 2
            # Map cx to column based on header positions

Steps:

  1. Extract header text positions to find column boundaries
  2. Assign each data block to a column by x-center
  3. Map y-positions to row labels using the hour/time column

Enumerating Vector Elements

Use page.get_drawings() to list all vector shapes — useful for identifying which drawings belong to which table area:

for i, d in enumerate(page.get_drawings()):
    rect = d.get('rect')
    fill = d.get('fill')
    if rect:
        print(f'[{i}] rect={rect} fill={fill}')

Pitfalls

  • Redaction kills grid lines. Redrawing a table cell removes the dividers inside it. Redraw them explicitly after redaction.
  • Hebrew text reversed without bidi. Always use get_display() from python-bidi for RTL scripts. insert_htmlbox with dir="rtl" CSS also fails — it renders LTR.
  • Font path is OS-specific. Tahoma: C:/Windows/Fonts/tahoma.ttf (Windows). On Linux/macOS use Noto Sans Hebrew or similar.
  • Save to same path fails. doc.save(path) errors if path == the opened file. Use incremental save or open from original source.
  • Verify with vision_analyze. Text extraction may show different order than visual rendering. Always render a crop and verify for layout-critical edits.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.