使用PyPDF更改PDF中的字段?

huangapple go评论123阅读模式
英文:

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(&quot;exmpl3.pdf&quot;)
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: &quot;some filled in text&quot;}
)

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

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

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

C:\DEV\Python-Diverses\pypdf&gt;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 &quot;C:\DEV\Python-Diverses\pypdf\exmpl3.py&quot;, line 13, in &lt;module&gt;
    writer.update_page_form_field_values(
  File &quot;C:\Users\WRSPOL\AppData\Local\Programs\Python\Python39\lib\site-packages\pypdf\_writer.py&quot;, line 946, in update_page_form_field_values
    raise PyPdfError(&quot;No /AcroForm dictionary in PdfWriter Object&quot;)
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 &#39;/AcroForm&#39; in existing_pdf.trailer[&#39;/Root&#39;].keys():
    writer._root_object.update({NameObject(&quot;/AcroForm&quot;): reader.trailer[&quot;/Root&quot;][&quot;/AcroForm&quot;]})

huangapple
  • 本文由 发表于 2023年7月31日 19:28:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定