Skip to content

Commit

Permalink
ROB: Ensure update_page_form_field_values does not fail if no fields (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
pubpub-zz authored Sep 18, 2022
1 parent 620d2fa commit 141a765
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,9 @@ def update_page_form_field_values(
"""
self.set_need_appearances_writer()
# Iterate through pages, update field values
if PG.ANNOTS not in page:
logger_warning("No fields to update on this page", __name__)
return
for j in range(len(page[PG.ANNOTS])): # type: ignore
writer_annot = page[PG.ANNOTS][j].get_object() # type: ignore
# retrieve parent field values, if present
Expand Down
6 changes: 6 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,17 @@ def test_fill_form():
page = reader.pages[0]

writer.add_page(page)
writer.add_page(PdfReader(RESOURCE_ROOT / "crazyones.pdf").pages[0])

writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}, flags=1
)

# check if no fields to fill in the page
writer.update_page_form_field_values(
writer.pages[1], {"foo": "some filled in text"}, flags=1
)

writer.update_page_form_field_values(
writer.pages[0], {"foo": "some filled in text"}
)
Expand Down

0 comments on commit 141a765

Please sign in to comment.