Skip to content

Add automated PDF form filler (fill_pdf_form.py) - #1

Draft
bowatts wants to merge 1 commit into
masterfrom
cursor/automate-pdf-form-filling-afca
Draft

bowatts wants to merge 1 commit into
masterfrom
cursor/automate-pdf-form-filling-afca

Conversation

@bowatts

@bowatts bowatts commented Jul 29, 2026

Copy link
Copy Markdown
Member

Summary

Adds a self-contained Python toolkit under PDF_Forms/ that automates populating fillable PDF forms when the field data is provided. Only one runtime dependency is needed: pypdf.


New files

File Purpose
PDF_Forms/fill_pdf_form.py Main CLI tool — inspect, single-fill, batch-fill
PDF_Forms/requirements.txt Runtime dependency (pypdf>=4.0.0)
PDF_Forms/sample_data.json Example single-record data file
PDF_Forms/sample_batch.csv Example multi-record batch file
PDF_Forms/sample_form.pdf Pre-built test template with 9 AcroForm text fields
PDF_Forms/create_sample_form.py Dev helper to regenerate the test PDF (needs reportlab)

Usage

Install

pip install -r PDF_Forms/requirements.txt

1 — Discover what fields a PDF has

python3 PDF_Forms/fill_pdf_form.py --inspect template.pdf

Output example:

Form fields in 'template.pdf'  (9 total)

  Field Name                               Type           Current Value
  ---------------------------------------------------------------------------
  FirstName                                /Tx            ''
  LastName                                 /Tx            ''
  Email                                    /Tx            ''
  ...

2 — Fill from a JSON file

python3 PDF_Forms/fill_pdf_form.py template.pdf --data data.json --output filled.pdf

data.json is a flat { "FieldName": "value" } object. Unknown keys are silently ignored so one data file can target multiple templates.

3 — Fill with inline key=value pairs

python3 PDF_Forms/fill_pdf_form.py template.pdf \
  --set "FirstName=Jane" --set "LastName=Doe" --set "[email protected]"

4 — Batch fill from a CSV (one PDF per row)

python3 PDF_Forms/fill_pdf_form.py template.pdf \
  --csv records.csv --output-dir ./filled/

CSV headers must match the PDF field names exactly. Output files are named <template>_<FirstValue>_<SecondValue>.pdf for easy identification.


Implementation notes

  • Uses PdfWriter.append() to clone the full document (pages + AcroForm metadata) before calling update_page_form_field_values() — this is the correct pypdf pattern that preserves form field annotations in the output.
  • Multi-page PDFs are handled: field values are applied to every page.
  • Output directories are created automatically.
Open in Web Open in Cursor 

Introduces PDF_Forms/ — a self-contained Python toolkit that
automatically populates fillable PDF forms from structured data.

Files added:
- fill_pdf_form.py   – main CLI: inspect fields, single-fill (JSON /
                       inline key=value), and batch-fill from CSV
- requirements.txt   – single dependency: pypdf>=4.0.0
- create_sample_form.py – helper to generate a test fillable PDF via
                          reportlab (not required at runtime)
- sample_data.json   – example single-record data file
- sample_batch.csv   – example multi-record batch file
- sample_form.pdf    – pre-built test template with 9 AcroForm fields

Usage summary:
  python3 fill_pdf_form.py --inspect template.pdf
  python3 fill_pdf_form.py template.pdf --data data.json
  python3 fill_pdf_form.py template.pdf --set 'Name=Alice' --set 'City=NY'
  python3 fill_pdf_form.py template.pdf --csv records.csv --output-dir ./out/

Co-authored-by: Bo Watts <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants