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?
from pypdf import PdfReader
print(bool(PdfReader("form-fillable.pdf").get_fields()))
print(bool(PdfReader("company-extract.pdf").get_fields()))True
FalseIf 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
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"))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 ProEach field has a name, a type and a value:
| Type | Meaning | Value |
|---|---|---|
/Tx | Text box | The text typed in |
/Btn | Checkbox or radio button | /Yes (or another name the form's author chose) when ticked, /Off when not |
/Ch | Drop-down or list | The 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:
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())])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,ProExcel 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:
Customer registration form
Full name
Company
Email
Company number
Send invoices by email
PlanThis 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.