Ask Anvil

Answers to questions about automating PDFs, e-signatures, Webforms, and other paperwork problems.
PDFs
Categories

Why do only the first page's fields get filled when I use pypdf, and how do I fix it?

You fill a multi-page PDF form with pypdf, the values on page 1 appear, and every field on page 2 and later is empty. No error is raised, which makes it easy to miss.

from pypdf import PdfReader, PdfWriter

writer = PdfWriter()
writer.append(PdfReader("form.pdf"))

values = {"name": "Ada Lovelace", "city": "London"}

# Passing None applies the values to every page
writer.update_page_form_field_values(None, values)

with open("filled.pdf", "wb") as f:
    writer.write(f)

Why it happens

The usual pattern in tutorials is writer.update_page_form_field_values(writer.pages[0], values). That call only updates the widgets on the page object you pass in. A field named city on page 2 is never visited, so it keeps its empty value. The pypdf docs describe the page argument as a page, a list of pages, or None, and say None processes all pages. In a test on pypdf 5.9 with a two-page form, passing only the first page left the page 2 field as an empty string, while passing None filled both.

The fix

Pass None as shown above, or loop over writer.pages and call the method once per page. Both produced the same result in the same test. Use the loop form if you want to pass different values to different pages.

Caveats

Field names are matched across the pages you pass, so two fields that share a name will receive the same value. Also check the result by reading the values back with PdfReader("filled.pdf").get_form_text_fields() instead of eyeballing page 1 only. By default the method also sets the NeedAppearances flag (auto_regenerate), which asks viewers to redraw the fields.

Back to All Questions

The fastest way to build software for documents

Anvil Document SDK is a comprehensive toolbox for product teams launching document flows where PDF filling, signing, and complex conditional scenarios are necessary.
Explore Anvil
Anvil Webforms
Marketing Mode