I have a template pdf https://www.irs.gov/pub/irs-pdf/f2848.pdf that I want to fill fields with csv data. My script is:
template = '..\\..\\02. Inputs\\f2848.pdf'
doc=fitz.open(template)
df = pd.read_csv('..\\..\\02. Inputs\\ 2848 Import.csv')
field_dictionary = df.to_dict('records')[0] # dictionary key matches field names
outfile = '..\\..\\04. Outputs\\2848 python testing.pdf'
for page in doc.pages():
for field in page.widgets():
for k,v in field_dictionary.items():
if field.field_name==k:
field.field_value=v
field.update()
page=doc.reload_page(page)
doc.need_appearances(value=True)
doc.save(outfile)
I opened the output file, the form is still blank not showing values, but when I examine the field values of the output file in the code, it returns updated value:
doc2 = fitz.open(outfile)
page2 =doc2[0]
for field1 in page2.widgets():
print(field1.field_value) # the print displays the value from the csv file.
I'm confused why the values are not showing on the form even though i set "need_appearance" of output file to true?
Would be appreciated if anyone could take a look. Thanks.