how*_*ese 4 python pdf checkbox pypdf
我有下面的代码
d = {'Name': 'James', ' Date':'1/1/2016','City':'Wilmo','County':'United States'}
input_file = PdfFileReader(open(baseDir + 'medicareRRF.pdf', "rb"))
inFields = input_file.getFields()
watermark = PdfFileReader(open(baseDir + "justSign.pdf", "rb"))
output_file = PdfFileWriter()
input_page = input_file.getPage(0)
input_page.mergePage(watermark.getPage(0))
output_file.addPage(input_page)
thisPage = output_file.getPage(0)
output_file.updatePageFormFieldValues(thisPage, d)
Run Code Online (Sandbox Code Playgroud)
哪个用字典(d)正确填写PDF,但是如何检查和取消选中PDF上的框?以下是其中一个框的getField()信息:
u'Are you ok': {'/FT': '/Btn','/Kids': [IndirectObject(36, 0),
IndirectObject(38, 0)],'/T': u'Are you ok','/V': '/No'}
Run Code Online (Sandbox Code Playgroud)
我尝试添加{'Are you ok' : '/Yes'}和其他几个类似的方法,但没有任何效果.
小智 9
我遇到了同样的问题,看了几个地方,并且对我找不到答案感到失望.经过几个令人沮丧的时间查看我的代码,pyPDF2代码和Adobe PDF 1.7规范后,我终于明白了.如果你调试到updatePageFormFieldValues,你会看到它只使用TextStringObjects.复选框不是文本字段 - 即使/ V值也不是文本字段,这对我来说似乎违反直觉.调试到该函数向我显示复选框是NameObjects,所以我创建了自己的函数来处理它们.我创建了两个dicts:一个只有文本值,我传递给内置的updatePageFormFieldValues函数,另一个只有复选框值.我还设置了/ AS以确保可见性(参见PDF规范).我的功能看起来像这样:
def updateCheckboxValues(page, fields):
for j in range(0, len(page['/Annots'])):
writer_annot = page['/Annots'][j].getObject()
for field in fields:
if writer_annot.get('/T') == field:
writer_annot.update({
NameObject("/V"): NameObject(fields[field]),
NameObject("/AS"): NameObject(fields[field])
})
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,您使用/ 1,/ On还是/ Yes取决于表单的定义方式或PDF阅读器的查找方式.对我来说,/ 1工作.
小智 5
我想补充一下@rpsip 的答案。
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import NameObject
reader = PdfReader(r"form2.pdf") #where you read the pdf in the same directory
writer = PdfWriter()
page = reader.pages[0] #read page 1 of your pdf
fields = reader.get_fields()
print (fields) # this is to identify if you can see the form fills in that page
writer.add_page(page) #this line is necessary otherwise the pdf will be corrupted
for i in range(len(page["/Annots"])): #in order to access the "Annots" key
print ((page["/Annots"][i].get_object())) #to find out which of the form fills are checkbox or text fill
if (page["/Annots"][i].get_object())['/FT']=="/Btn" and (page["/Annots"][i].get_object())['/T']=='Check Box3': #this is my filter so that I can filter checkboxes and the checkbox I want i.e. "Check Box 3"
print (page["/Annots"][i].get_object()) #further check if I got what I wanted as per the filter
writer_annot = page["/Annots"][i].get_object()
writer_annot.update(
{
NameObject("/V"): NameObject(
"/Yes"), #NameObject being only for checkbox, and please try "/Yes" or "/1" or "/On" to see which works
NameObject("/AS"): NameObject(
"/Yes" #NameObject being only for checkbox, and please try "/Yes" or "/1" or "/On" to see which works
)
}
)
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream) #save the ticked pdf file as another file named "filled-out.pdf"
Run Code Online (Sandbox Code Playgroud)
希望我有所帮助。