我对PyPDF2相当陌生,我主要使用的是在网上找到的代码片段。我要做的只是填写使用Adobe Acrobat XI Pro创建的PDF表单。虽然它可以完美地与文本字段配合使用,但我无法设置下拉列表的值。
我能够确定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()(https://github.com/mstamy2/PyPDF2/blob/master/PyPDF2/pdf.py#L354)的源代码,您会发现它非常简单。找到对象后,您可以执行以下操作:
obj.update({NameObject('/FT'): NameObject('/Tx')})
您可以保存修改后的pdf文件,以后再填充文件,或者您可以先将对象类型更新为文本字段,然后直接填充修改后的字段。