PyPDFForm · fill a PDF form in Python in a few lines

4 months ago

Filling a PDF form in code usually means fighting the PDF spec. PyPDFForm turns it into ordinary Python. Inspect a form to see its fields, fill it from a dictionary, and save the result, all in a few lines.

Ask about this presentation

Chapters

  1. 0:00Slide 1
  2. 0:13A library and a command line tool
  3. 0:24Install it
  4. 0:29First, see what the form has
  5. 0:40Fill it by name
  6. 0:53Write it out
  7. 1:06PDF forms, as plain Python
Show transcript
  1. Filling a PDF form in code usually means fighting the PDF spec. PyPDFForm turns it into ordinary Python. Inspect a form to see its fields, fill it from a dictionary, and save the result, all in a few lines.

    Slide text
    PyPDFForm logo

    Create, inspect, style, and fill PDF forms in Python.

    inspect → fill → save
  2. It is a Python library and a command line tool for PDF forms. From one object you create forms, inspect them, restyle their fields, and fill them. It also merges PDFs and extracts pages.

    Slide text
    The PyPDFForm CLI filling a PDF form

    Create, inspect, restyle, and fill forms · plus merge PDFs and extract pages.

  3. Install the library with pip. Want the command line tool too? Install it with pipx and the CLI extra.

    Slide text
    pip install PyPDFForm          # the library
    pipx install "PyPDFForm[cli]"  # + the CLI

    The library is all you need for Python · add the CLI extra for the command line tool.

  4. You fill fields by name, so first see what the form holds. Wrap your PDF and print its schema. You get a clean JSON schema of every field and its type. Here, three text fields and three checkboxes.

    Slide text
    from PyPDFForm import PdfWrapper
    print(PdfWrapper("sample_template.pdf").schema)
    {
      "type": "object",
      "properties": {
        "test":    {"type": "string"},
        "check":   {"type": "boolean"},
        "test_2":  {"type": "string"},
        "check_2": {"type": "boolean"},
        "test_3":  {"type": "string"},
        "check_3": {"type": "boolean"}
      }
    }
  5. Now fill it. Pass a dictionary of field name to value. Strings go into text fields, and booleans tick the checkboxes. Add flatten equals True, and the filled values are locked, so no one can edit them later.

    Slide text
    from PyPDFForm import PdfWrapper
    
    filled = PdfWrapper("sample_template.pdf").fill({
        "test": "test_1",   "check": True,
        "test_2": "test_2", "check_2": False,
        "test_3": "test_3", "check_3": True,
    })

    Strings fill text fields, booleans tick checkboxes · pass flatten=True to lock the values.

  6. Write it out, and you have a finished PDF. Here is the blank template, and here it is filled: the text field now shows a value and the checkbox is ticked. Prefer no code? The command line tool runs the same fill from a data file in one line.

    Slide text
    filled.write("output.pdf")
    Blank sample template PDF
    sample_template.pdf
    Filled sample PDF
    output.pdf · text filled, checkbox ticked

    No code? The CLI does the same fill from a data file: pypdfform fill sample_template.pdf -f data.yaml

  7. That is the first win: inspect, fill, and save. The same object also builds forms from scratch, restyles fields, adds signatures and images, and handles radios, dropdowns, merging, and page extraction. Install it with pip and start from the docs.

    Slide text
    • Inspect · schema, metadata, and current values.
    • Fill · text, checkboxes, radios, dropdowns, images, signatures.
    • Create · build forms and fields from scratch.
    • Utilities · merge PDFs and extract pages.

    pip install PyPDFForm  ·  chinapandaman.github.io/PyPDFForm

What happened in AI today, in your inbox

One short email each evening. Read it and you’re caught up.

Built with ❤️ in New York