英文:
Change fields in pdf using pypdf?
问题
我尝试使用以下Python代码和pyPdf模块来更新PDF中的输入字段。首先,我读取PDF文件并获取第一个页面上的所有可用字段。
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: "填写的文本内容"}
)
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream)
但是,当我运行此程序时,我收到以下错误消息:
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]
...
如何更新此PDF中的字段?
英文:
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?
答案1
得分: 1
使用相同的方法解决了这个问题。我所做的是使用先前版本的 pypdf
。
pip install pypdf==3.8.1
英文:
Had the same issue. What I did was to use a previous version of pypdf
.
pip install pypdf==3.8.1
答案2
得分: 1
考虑在尝试更新页面表单字段之前,将原始PDF中的/Acroform对象复制到新PDF中。
if '/AcroForm' in existing_pdf.trailer['/Root'].keys():
writer._root_object.update({NameObject("/AcroForm"): reader.trailer["/Root"]["/AcroForm"]})
英文:
Consider copying the /Acroform object from your original pdf to your new one before attempting to update the page form fields.
if '/AcroForm' in existing_pdf.trailer['/Root'].keys():
writer._root_object.update({NameObject("/AcroForm"): reader.trailer["/Root"]["/AcroForm"]})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论