选择Python(PYPDF2)的PDF中的复选框

发布于 2025-02-05 11:14:31 字数 2627 浏览 4 评论 0 原文

我正在尝试使填充此PDF自动化: (用ilovepdf解密)。

我对文本没有任何问题,但是使用复选框没有办法。许多 /btn有 /孩子 /孩子是其他复选框,看起来为“ indirectobject”。 另一个复选框,我无法选择/修改,我无法在此PDF(示例)

代码

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
from collections import OrderedDict

def set_need_appearances_writer(writer: PdfFileWriter):
    # See 12.7.2 and 7.7.2 for more information: http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
    try:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update({
                NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer)
            })

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
        # del writer._root_object["/AcroForm"]['NeedAppearances']
        return writer

    except Exception as e:
        print('set_need_appearances_writer() catch : ', repr(e))
        return writer

reader = PdfFileReader("TEMPORAL COMPLETO12 de mayo_unlocked.pdf")
writer = PdfFileWriter()

set_need_appearances_writer(writer)

page = reader.pages[0]

writer.addPage(page)

#Texto4 works, but not the checkboxes
writer.updatePageFormFieldValues(
    writer.getPage(0), {'BOTON_TIPOJORNADA': '/1',
                        'BOTON_JORN': '/S',
                        'Texto4': 'Texto4'
                        }
)
with open("filled-out.pdf", "wb") as output_stream:
    writer.write(output_stream)
reader.stream.close()

另外,如果我手动修改PDF并读取字段...:

reader.getFields()

OUTPUT (one checkbox selected):
[...]

'BOTON_JORN': {'/FT': '/Btn',
  '/Kids': [IndirectObject(160, 0),
   IndirectObject(162, 0),
   IndirectObject(167, 0),
   IndirectObject(172, 0)],
  '/T': 'BOTON_JORN',
  '/Ff': 49152,
  '/V': '/S'},

OUTPUT (another checkbox selected):
[...]

'BOTON_JORN': {'/FT': '/Btn',
  '/Kids': [IndirectObject(160, 0),
   IndirectObject(162, 0),
   IndirectObject(167, 0),
   IndirectObject(172, 0)],
  '/T': 'BOTON_JORN',
  '/Ff': 49152,
  '/V': '/D'},

中选择/修改普通复选框。 “ textocasilla deverificación25”当选择具有值时'/s#ed'

'TEXTOCasilla de verificación25': {'/FT': '/Btn',
  '/T': 'TEXTOCasilla de verificación25',
  '/V': '/S#ED'},

任何提示都很棒

I'm trying to automate filling this PDF:
https://www.sepe.es/SiteSepe/contenidos/empresas/contratos_trabajo/asistente/pdf/temporal/Temporal.pdf
(decrypt it with ilovepdf).

I have no problem with the text, but with the checkboxes there is no way. Many /Btn have /Kids those /kids are other checkboxes that appear as "indirectObject". Also, normal checkboxes I can't select/modify in this pdf (examples bellow)

Code

from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject
from collections import OrderedDict

def set_need_appearances_writer(writer: PdfFileWriter):
    # See 12.7.2 and 7.7.2 for more information: http://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
    try:
        catalog = writer._root_object
        # get the AcroForm tree
        if "/AcroForm" not in catalog:
            writer._root_object.update({
                NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer)
            })

        need_appearances = NameObject("/NeedAppearances")
        writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True)
        # del writer._root_object["/AcroForm"]['NeedAppearances']
        return writer

    except Exception as e:
        print('set_need_appearances_writer() catch : ', repr(e))
        return writer

reader = PdfFileReader("TEMPORAL COMPLETO12 de mayo_unlocked.pdf")
writer = PdfFileWriter()

set_need_appearances_writer(writer)

page = reader.pages[0]

writer.addPage(page)

#Texto4 works, but not the checkboxes
writer.updatePageFormFieldValues(
    writer.getPage(0), {'BOTON_TIPOJORNADA': '/1',
                        'BOTON_JORN': '/S',
                        'Texto4': 'Texto4'
                        }
)
with open("filled-out.pdf", "wb") as output_stream:
    writer.write(output_stream)
reader.stream.close()

If I modified the pdf manually and read the fields...:

reader.getFields()

OUTPUT (one checkbox selected):
[...]

'BOTON_JORN': {'/FT': '/Btn',
  '/Kids': [IndirectObject(160, 0),
   IndirectObject(162, 0),
   IndirectObject(167, 0),
   IndirectObject(172, 0)],
  '/T': 'BOTON_JORN',
  '/Ff': 49152,
  '/V': '/S'},

OUTPUT (another checkbox selected):
[...]

'BOTON_JORN': {'/FT': '/Btn',
  '/Kids': [IndirectObject(160, 0),
   IndirectObject(162, 0),
   IndirectObject(167, 0),
   IndirectObject(172, 0)],
  '/T': 'BOTON_JORN',
  '/Ff': 49152,
  '/V': '/D'},

Another checkbox, with NO /kids but I can't select/modify is:
'TEXTOCasilla de verificación25' when selected has the value '/S#ED'

'TEXTOCasilla de verificación25': {'/FT': '/Btn',
  '/T': 'TEXTOCasilla de verificación25',
  '/V': '/S#ED'},

Any tip will be great

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2025-02-12 11:14:32

总是困惑程序员为什么需要销毁形式模板,它是一个空白的画布,没有程序只有观众/打印机,因此您可以填充FDF并打开FDF,将自动编写PDF,并用所有新的字段输入值,包括按钮,复选框或其他选择。

%FDF-1.4
%âãÏÓ
1 0 obj
<<
/FDF <<
/F (TEMPORAL COMPLETO12 de mayo.pdf)
/Fields [<<
/T (TEXTO Casilla de verificación 480)
/V /Off
>> <<
/T (TEXTO Casilla de verificación 481)
/V /Off
>> <<
/T (TEXTO20369)
/V /Off
>> <<
/T (TEXTOCasilla de verificación106666)
/V /Off
...
<<
/T (TEXTOCasilla de verificación95555)
/V /Off
>> <<
/T (Texto4)

/v(我对PDF的输入值)

>> <<
/T (Textocasilla de verificación3)
/V /Off
>> <<
/T (Textocasilla de verificación30)
/V /Off
>>]
/ID [<25F5DFD17199935FF41213A08FEAFF84> <9D8212B1DE099CB9D6BC87991799EBC3>]
/UF (TEMPORAL COMPLETO12 de mayo.pdf)
>>
/Type /Catalog
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

“在此处输入图像说明”

包括Python的库,包括Python的库来导出FDF,以通过服务器或命令行填充fdf (甚至通过MS Notepad进行手动编辑)或查找并替换文本值。有关一个有力的例子,请参见 https://www.pdftron.com/documentation com.com/documentation/samampels/ py/fdftest/但是,在github上,搜索 fdf pdf 应该扔掉数十个。

注意以上样本将需要过度限制的导出限制。

写作或编辑文本FDF条目后,只需打开FDF即使限制了相关的空白模板。

在这里打开数据文件,我需要确认其允许的

”在此处输入图像描述

但我们可以看到我编写的数据(在这种情况下为字段名称),已导入

​: - 在导入并非每个字段都需要添加时,但是您确实需要根据Resave填充所有字段。

Always confused why programmers need to destroy form templates, it is a blank canvas to be populated without a program only a viewer/printer so you fill the FDF and opening the FDF will automatically write the PDF, with all the new field entry values, including buttons, check boxes or other selections.

%FDF-1.4
%âãÏÓ
1 0 obj
<<
/FDF <<
/F (TEMPORAL COMPLETO12 de mayo.pdf)
/Fields [<<
/T (TEXTO Casilla de verificación 480)
/V /Off
>> <<
/T (TEXTO Casilla de verificación 481)
/V /Off
>> <<
/T (TEXTO20369)
/V /Off
>> <<
/T (TEXTOCasilla de verificación106666)
/V /Off
...
<<
/T (TEXTOCasilla de verificación95555)
/V /Off
>> <<
/T (Texto4)

/V (My Inputted value for the PDF)

>> <<
/T (Textocasilla de verificación3)
/V /Off
>> <<
/T (Textocasilla de verificación30)
/V /Off
>>]
/ID [<25F5DFD17199935FF41213A08FEAFF84> <9D8212B1DE099CB9D6BC87991799EBC3>]
/UF (TEMPORAL COMPLETO12 de mayo.pdf)
>>
/Type /Catalog
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

enter image description here

There are many libraries including Python ones to export the FDF for populating via server or command line (even manual edit via MS NotePad) or Find and Replace a text value. For a powerful example see https://www.pdftron.com/documentation/samples/py/FDFTest/ but on github a search of FDF PDF should throw up dozens.

Note the sample above will need the export restriction over-ruled.

Once you write or edit the text FDF entries, then simply opening the FDF will auto populate the associated blank template.pdf, even if it was restricted.

Here opening data file I need to confirm its allowed

enter image description here

But we can see the data I write (in this case the field name), has been imported

enter image description here

Note:- when importing not every field needs to be added, but you do need to fill all fields as required for a resave.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文