Python PyPDF2在不使用字典的情况下更改字段值

Python PyPDF2在不使用字典的情况下更改字段值,python,python-3.x,pdf-generation,pypdf2,pypdf,Python,Python 3.x,Pdf Generation,Pypdf2,Pypdf,我对PyPDF2非常陌生,我主要使用在网上找到的代码片段。我所做的只是填写PDF格式创建的Adobe AcROAT席XPro。虽然它可以完美地处理文本字段,但我在设置下拉列表的值时遇到了麻烦 我能够确定PyPDF2看到的是: {'/FT': '/Ch', '/T': DocumentType', '/Ff': 4325378, '/V': 'D', '/DV': 'W'} 对于文本字段,它显示的是: {'/FT': '/Tx', '/T': 'SupervisorName', '/Ff': 2

我对PyPDF2非常陌生,我主要使用在网上找到的代码片段。我所做的只是填写PDF格式创建的Adobe AcROAT席XPro。虽然它可以完美地处理文本字段,但我在设置下拉列表的值时遇到了麻烦

我能够确定PyPDF2看到的是:

{'/FT': '/Ch', '/T': DocumentType', '/Ff': 4325378, '/V': 'D', '/DV': 'W'}
对于文本字段,它显示的是:

{'/FT': '/Tx', '/T': 'SupervisorName', '/Ff': 29360130}
但我还没有找到类似的方法来更新这些值。如何在此处直接操作/更新/V的值?

处理我的PDF的代码如下所示:

def set_need_appearances_writer(writer):
    # 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 and add "/NeedAppearances attribute
        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)
        return writer

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


def pdf_handling(f_template_file, f_output_file, f_field_dict):
    inputStream = open(f_template_file, "rb")
    pdf_reader = PdfFileReader(inputStream, strict=False)
    if "/AcroForm" in pdf_reader.trailer["/Root"]:
        pdf_reader.trailer["/Root"]["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    pdf_writer = PdfFileWriter()
    set_need_appearances_writer(pdf_writer)
    if "/AcroForm" in pdf_writer._root_object:
        pdf_writer._root_object["/AcroForm"].update(
            {NameObject("/NeedAppearances"): BooleanObject(True)})

    pdf_writer.addPage(pdf_reader.getPage(0))
    pdf_writer.updatePageFormFieldValues(pdf_writer.getPage(0), f_field_dict)

    outputStream = open(f_output_file, "wb")
    pdf_writer.write(outputStream)

    inputStream.close()
    outputStream.close()
用价值观来称呼它:

field_dict = {
    'IssueDay': DDay,
    'IssueMonth': MMonth,
    'IssueYear': YYear,
    'RecruitmentNumber': row['RecruitmentID'].zfill(5),
    'DocumentType': 'D',
}

template_file = os.path.join(template_path, 'document_template.pdf')
output_file = os.path.join(person_path, 'document_output.pdf')

pdf_handling(template_file, output_file, field_dict)

我尝试使用PyPDF2操作下拉列表,但找不到解决此问题的方法。我找到了一个解决办法,基本上就是把下拉列表变成文本字段,然后你可以像其他任何文本字段一样填写任何你想要的文本

为此,您需要定位对象,并将“/FT”字段从“/Ch”更新为“/Tx”。如果您查看updatePageFormFieldValues()的源代码,您会发现这非常简单。找到对象后,可以执行以下操作:

obj.update({NameObject('/FT'): NameObject('/Tx')})
您可以保存修改后的pdf文件,稍后再填充该文件,也可以先将对象类型更新为文本字段,然后直接填充修改后的字段