Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change fields in pdf using pypdf?

Tags:

python

pypdf

i try to update entry-fields in a pdf using the following code with the pytho-module pypdf. At first i read the pdf-files and get all available fields on this firt pdf-page.

from pypdf import PdfReader, PdfWriter

reader = PdfReader("exmpl3.pdf")
writer = PdfWriter()

page = reader.pages[0]
fields = reader.get_fields()
for k,v in enumerate(fields):
  print (k, v)

writer.add_page(page)

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

with open("filled-out.pdf", "wb") as output_stream:
  writer.write(output_stream)

But when i run this program i get the following error-message:

C:\DEV\Python-Diverses\pypdf>python exmpl3.py
Traceback (most recent call last):
  File "C:\DEV\Python-Diverses\pypdf\exmpl3.py", line 13, in <module>
    writer.update_page_form_field_values(
  File "C:\Users\WRSPOL\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py", line 946, in update_page_form_field_values
    raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
pypdf.errors.PyPdfError: No /AcroForm dictionary in PdfWriter Object

C:\DEV\Python-Diverses\pypdf>python exmpl3.py
0 form1[0].#subform[0].TextField1[0]
1 form1[0].#subform[0].TextField1[1]
2 form1[0].#subform[0].TextField1[2]
3 form1[0].#subform[0].TextField1[3]
4 form1[0].#subform[0].TextField1[4]
5 form1[0].#subform[0].TextField1[5]
6 form1[0].#subform[0].TextField1[6]
7 form1[0].#subform[0].TextField1[7]
8 form1[0].#subform[0].TextField1[8]
9 form1[0].#subform[0].CheckBox1[0]
10 form1[0].#subform[0].CheckBox2[0]
11 form1[0].#subform[0].CheckBox3[0]
12 form1[0].#subform[0].CheckBox4[0]
13 form1[0].#subform[0].CheckBox5[0]
14 form1[0].#subform[0].SSN[0]
15 form1[0].#subform[0].HouseholdIncome1[0]
16 form1[0].#subform[0].HouseholdIncome1[1]
17 form1[0].#subform[0].HouseholdIncome2[0]
18 form1[0].#subform[0].HouseholdIncome3[0]
19 form1[0].#subform[0].DateSigned[0]
20 form1[0].#subform[0].SignatureField1[0]
21 form1[0].#subform[0]
22 form1[0].#subform[1].Text38[0]
23 form1[0].#subform[1].RadioButtonList[0]
24 form1[0].#subform[1].DateRecordUpdated[0]
25 form1[0].#subform[1].DateSigned[1]
26 form1[0].#subform[1].SignatureField1[1]
27 form1[0].#subform[1].DateNotified[0]
28 form1[0].#subform[1]
29 form1[0]
Traceback (most recent call last):
  File "C:\DEV\Python-Diverses\pypdf\exmpl3.py", line 13, in <module>
    writer.update_page_form_field_values(
  File "C:\Users\WRSPOL\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py", line 946, in update_page_form_field_values
    raise PyPdfError("No /AcroForm dictionary in PdfWriter Object")
pypdf.errors.PyPdfError: No /AcroForm dictionary in PdfWriter Object

How can i update the fields in this pdf?

like image 349
Rapid1898 Avatar asked Jul 13 '26 12:07

Rapid1898


2 Answers

The following code copies all the root objects (/AcroForm is a root object) from the reader to the writer fixing the raised PyPdfError("No /AcroForm dictionary in PdfWriter Object")

writer.clone_reader_document_root(reader)

like image 76
Alex Ricciardi Avatar answered Jul 15 '26 02:07

Alex Ricciardi


Had the same issue. What I did was to use a previous version of pypdf.

pip install pypdf==3.8.1

like image 44
mch22 Avatar answered Jul 15 '26 02:07

mch22