Skip to content

How to Fill PDF Forms Programmatically with Python

I need to fill hundreds of PDF forms automatically - invoices, contracts, government forms. Manual filling is impractical and error-prone. Let me show you how to automate this with Python.

Understanding PDF Form Fields

PDF forms contain several field types, each requiring different handling:

TypeDescriptionValue Format
TextSingle/multi-line textString
CheckboxBoolean selectiontrue/false
RadioSingle choice from groupOption value
DropdownSelect from listOption value

Inspecting Form Fields

First, I need to see what fields exist in my PDF:

inspect_form.py
from pdf_oxide import PdfDocument
def inspect_form(pdf_path: str):
doc = PdfDocument(pdf_path)
fields = doc.get_form_fields()
print(f"Found {len(fields)} form fields:\n")
for field in fields:
print(f"Name: {field.name}")
print(f" Type: {field.field_type}")
print(f" Value: {field.value}")
if field.options:
print(f" Options: {field.options}")
inspect_form("tax_form.pdf")

Filling a Single Form

Once you know the field names, filling is straightforward:

fill_form.py
from pdf_oxide import PdfDocument
doc = PdfDocument("w2_form.pdf")
# Set field values
doc.set_form_field_value("employee_name", "Jane Doe")
doc.set_form_field_value("wages", "85000.00")
# Save the filled form
doc.save("filled_w2.pdf")

Handling Different Field Types

Each field type needs different handling:

field_types.py
from pdf_oxide import PdfDocument
doc = PdfDocument("application.pdf")
# Text field
doc.set_form_field_value("applicant_name", "John Smith")
# Checkbox (boolean)
doc.set_form_field_value("agree_terms", True)
# Dropdown (must be valid option)
doc.set_form_field_value("state", "California")
# Radio button (value from group)
doc.set_form_field_value("gender", "male")
doc.save("filled_application.pdf")

Batch Filling from CSV

Process multiple records from a spreadsheet:

batch_fill.py
from pathlib import Path
from pdf_oxide import PdfDocument
import csv
def batch_fill_forms(template_path: str, data_csv: str, output_dir: Path):
output_dir.mkdir(exist_ok=True)
with open(data_csv) as f:
reader = csv.DictReader(f)
for row in reader:
doc = PdfDocument(template_path)
# Fill fields from CSV row
for field_name, value in row.items():
if field_name not in ("output_filename",):
doc.set_form_field_value(field_name, value)
# Save with unique filename
output_name = row.get("output_filename", f"form_{row.get('id', 'unknown')}.pdf")
doc.save(str(output_dir / output_name))
print(f"Created: {output_name}")
# Usage
batch_fill_forms(
"employee_form_template.pdf",
"employee_data.csv",
Path("filled_forms/")
)

Error Handling

Always wrap form operations in try-except:

safe_fill.py
from pdf_oxide import PdfDocument
def safe_fill_form(template_path: str, data: dict, output_path: str):
try:
doc = PdfDocument(template_path)
for field_name, value in data.items():
try:
doc.set_form_field_value(field_name, value)
except Exception as e:
print(f"Warning: Could not set {field_name}: {e}")
doc.save(output_path)
return True
except Exception as e:
print(f"Error filling form: {e}")
return False

Summary

In this post, I showed how to fill PDF forms programmatically with Python. The key point is using pdf_oxide’s simple API for form field manipulation.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments