How to extract data from PDF forms

Updated · Tested with Python 3.13, pypdf 7.2.0, PyMuPDF 2.2, pdfplumber 0.11.11

In short

A fillable PDF form stores each answer in a named field. Read them with pypdf's get_fields(), which returns every field's name, type and value. Do not use extract_text() for this: most libraries leave the answers out of the page text. A flat or scanned form has no fields, and its answers have to be found by label or by position.

PDF forms come in two kinds, and they could not be more different to read. A fillable form stores each answer in a named field. A flat form is just a page: the answers are ordinary text, or ink in a scan. The example file, form-fillable.pdf, is fillable, with four text boxes, a checkbox and a drop-down list.

First: is it fillable?

Python
from pypdf import PdfReader

print(bool(PdfReader("form-fillable.pdf").get_fields()))
print(bool(PdfReader("company-extract.pdf").get_fields()))
Output
True
False

If a form was filled in and then "printed to PDF" or flattened, it has no fields any more, and the second half of this guide applies.

Read every field

Python
from pypdf import PdfReader

reader = PdfReader("form-fillable.pdf")
for name, field in reader.get_fields().items():
    print(name, field.get("/FT"), field.get("/V"))
Output
full_name /Tx Dana Levi
company /Tx Harbor Lane Logistics Ltd
email /Tx [email protected]
company_number /Tx 51-482913-6
invoices_by_email /Btn /Yes
plan /Ch Pro

Each field has a name, a type and a value:

TypeMeaningValue
/TxText boxThe text typed in
/BtnCheckbox or radio button/Yes (or another name the form's author chose) when ticked, /Off when not
/ChDrop-down or listThe chosen option

The checkbox is the one to watch: "ticked" is whatever name the author gave that state, often /Yes or /On. Treat anything other than /Off as ticked.

For text boxes only, reader.get_form_text_fields() returns a plain dictionary of name and value.

The field names are the form's, not yours

Names such as full_name come from whoever built the form, and many forms carry names like Text1 or topmostSubform[0].Page1[0].f1_01[0]. Print the fields of one filled-in copy and write down which name holds which answer. That mapping is the only setup a fillable form needs, and it stays valid for every copy of that form.

From many forms to a spreadsheet

One row per form, one column per field:

Python
import csv
import sys
from pypdf import PdfReader

paths = ["form-fillable.pdf"]
writer = csv.writer(sys.stdout)
for index, path in enumerate(paths):
    fields = PdfReader(path).get_fields()
    if index == 0:
        writer.writerow(["file", *fields.keys()])
    writer.writerow([path, *(str(field.get("/V")) for field in fields.values())])
Output
file,full_name,company,email,company_number,invoices_by_email,plan
form-fillable.pdf,Dana Levi,Harbor Lane Logistics Ltd,[email protected],51-482913-6,/Yes,Pro

Excel opens the result directly. For a folder, replace the list with sorted(glob.glob("forms/*.pdf")).

Why extract_text() misses the answers

The values of a fillable form are not part of the page's text. pypdf's extract_text() on this file returns only the printed labels:

Output
Customer registration form
Full name
Company
Email
Company number
Send invoices by email
Plan

This surprises people, because the answers are plainly visible on screen. We checked four libraries: pypdf, pdfplumber and pdfminer.six leave the values out of the text, and PyMuPDF includes them. If a pipeline "loses" the answers on forms, this is why. Read the fields, as above.

Flat forms

A flat form has no fields to read. What you can do depends on how it was made:

  • Filled in on a computer, then flattened. The answers are ordinary text on the page. Extract the text and find each answer by its label or by its position; text with coordinates shows how.
  • Printed, filled in by hand, and scanned. The page is a picture. OCR reads typed and printed answers well and handwriting poorly. Checkboxes are not text at all: you find them by looking at whether the box at a known position is dark.

For a form whose layout never changes, positions are dependable; template-based extraction goes through that approach and where it fails.

Forms and 100sands

Know the limit first: like most text extractors, 100sands does not read the values of fillable fields. For the sample above, /v1/text returns the printed labels and not the answers. For fillable forms, use pypdf as shown here; it is exact and takes a few lines.

Where 100sands fits is flat forms that were filled in on a computer, so the answers are text on the page. You set up a document type once from one to five filled-in copies: AI suggests the fields and their values, you check them, and each later copy comes back as named values in JSON, with a yes/no field as true or false. It does not read scanned or photographed forms, and it does not read handwriting. The PDF data extraction API page has the call.

Questions

How do I read form fields from a PDF in Python?

Open the file with pypdf's PdfReader and call get_fields(). Each entry has the field's name, its type in /FT and its value in /V. For text boxes only, get_form_text_fields() returns a simple dictionary.

Why does extract_text() not include the form's answers?

Field values are stored apart from the page's text. In our test pypdf, pdfplumber and pdfminer.six left them out and PyMuPDF included them. Read the fields directly.

How do I export fillable PDF form data to Excel?

Read each form's fields with pypdf and write one row per form to a CSV file with Python's csv module, using the field names as the header. Excel opens the CSV directly.

How do I know if a checkbox is ticked?

Its value is /Off when empty and another name, usually /Yes or /On, when ticked. Treat anything other than /Off as ticked.

Try it on your own documents

A free plan every month, no credit card. See the PDF data extraction API.

Start free