0

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.

katy
  • 25
  • 4
  • Maybe first use `print()` (and `print(type(...))`, `print(len(...))`, etc.) to see which part of code is executed and what you really have in variables. It is called `"print debuging"` and it helps to see what code is really doing. – furas Sep 16 '22 at 19:09
  • you could check `print( field.field_name, k, field.field_name==k )` to see if it `True` for any values. If it is always `False then it doesn't put any value in form. – furas Sep 16 '22 at 19:10
  • your code works for me - I can see values in new file. Maybe problem is program which you use to display PDF. I open it in web browser Firefox and in default viewer in Linux. – furas Sep 16 '22 at 19:48
  • @furas thank you for looking into it! You're right if i switched from Adobe to Chrome browser the values show up, not sure why Adobe doesn't work for this form. – katy Sep 16 '22 at 20:37

0 Answers0