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.

Easy Python 3.9+ Aug 9, 2026 Automation & scripting 11 views 0 copies

Python code

38 lines
Python 3.9+
import 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

stdout
{
  "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

  1. Use template.update({k: v for k, v in data.items() if k in template}) for a one-liner.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Automation & scripting

Related tutorials and quizzes for this topic.