Fill PDF Form Fields from a Mock Template in Python
Fills a PDF-style form template dictionary with user data, preserving template fields and formatting output as JSON.
Python code
38 linesimport json
template = {
"first_name": "",
"last_name": "",
"email": "",
"phone": "",
"date_of_birth": "",
"address": "",
"city": "",
"state": "",
"zip_code": "",
"agree_to_terms": False
}
def fill_pdf_form(template: dict, data: dict) -> dict:
for key, value in data.items():
if key in template:
template[key] = value
return template
if __name__ == "__main__":
user_data = {
"first_name": "Alice",
"last_name": "Johnson",
"email": "alice@example.com",
"phone": "555-123-4567",
"date_of_birth": "1990-05-15",
"address": "123 Main St",
"city": "Springfield",
"state": "IL",
"zip_code": "62701",
"agree_to_terms": True
}
completed_form = fill_pdf_form(template.copy(), user_data)
print(json.dumps(completed_form, indent=2))
Output
{
"first_name": "Alice",
"last_name": "Johnson",
"email": "alice@example.com",
"phone": "555-123-4567",
"date_of_birth": "1990-05-15",
"address": "123 Main St",
"city": "Springfield",
"state": "IL",
"zip_code": "62701",
"agree_to_terms": true
}
How it works
This function starts with a blank template dict and updates only keys present in both the template and the supplied data, preventing unknown keys from being added. By passing template.copy() to the function, the original template stays intact for reuse across multiple submissions. The function returns the same existing dictionary object, so callers can chain further processing or serialization without losing data. This pattern mirrors PDF form filling where only known fields are populated and the rest remain as defaults.
Common mistakes
- Calling fill_pdf_form(template, data) without .copy(), which permanently mutates the original template.
- Forgetting that the function updates the template in place and returns it, so passing the same dict twice yields stale data.
- Assuming unknown keys in data are added—this code silently ignores them.
Variations
- Use template.update({k: v for k, v in data.items() if k in template}) for a one-liner.
- Return template.copy() after filling so callers never share mutable state.
Real-world use cases
- Pre-filling government or insurance PDF forms from a CSV of applicant records before generating final documents.
- Populating survey templates in a kiosk app where users only submit fields present in the static form definition.
- Testing form-submission endpoints by building payloads from a canonical field list rather than trusting API input.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.