| name | pdf-form-filling |
| description | Fill PDF form fields programmatically using Python libraries like pypdf or pdfrw |
PDF Form Filling with Python
Overview
This skill covers how to identify and fill form fields in PDF documents, particularly for legal forms like court documents.
Key Libraries
1. PyPDF (pypdf)
- Modern, actively maintained library for PDF manipulation
- Can read form field names and fill them
- Installation:
pip install pypdf
2. pdfrw
- Lightweight alternative, good for form filling
- Installation:
pip install pdfrw
Common Workflow
With pypdf:
from pypdf import PdfReader, PdfWriter
reader = PdfReader("/path/to/form.pdf")
writer = PdfWriter()
fields = reader.get_fields()
print(fields.keys())
for page in reader.pages:
writer.add_page(page)
writer.update_page_form_field_values(
writer.pages[0],
{
"Field1": "Value1",
"Field2": "Value2",
}
)
with open("/path/to/filled.pdf", "wb") as f:
writer.write(f)
With pdfrw:
from pdfrw import PdfReader, PdfWriter, PdfDict, PdfObject
template = PdfReader("/path/to/form.pdf")
for field in template.Root.AcroForm.Fields:
print(field.T)
for field in template.Root.AcroForm.Fields:
if field.T == "FieldName":
field.V = "Field Value"
field.AP = None
PdfWriter().write("/path/to/filled.pdf", template)
Inspecting Form Fields
from pypdf import PdfReader
reader = PdfReader("form.pdf")
fields = reader.get_fields()
for field_name, field_data in fields.items():
print(f"{field_name}: {field_data}")
Important Notes
- Form field names are case-sensitive
- Some PDFs have checkboxes/radio buttons requiring "Yes"/"No" or specific values
- Date fields may need specific formatting
- Always check field types before filling
- Keep a copy of the blank form for reference
California Court Forms Specifics
California court forms often use:
- Multi-page layouts with page numbers
- Standard field naming conventions
- Checkboxes for yes/no selections
- Date fields in specific formats
- Signature/initial areas (typically left blank)
When filling:
- Always read the form first to understand field names
- Map case data to appropriate fields
- Leave court-filled sections empty (dates, case numbers, judge info)
- Preserve form formatting