How to fill PDF forms using Python
Solution 1
You can find the form fields here:
pdf.Root.AcroForm.Fields
or here
pdf.Root.Pages.Kids[page_index].Annots
This is a PdfArray object. Basically a List. The Name of the field is found here:
pdf.Root.AcroForm.Fields[field_index].T
Other keys include the value .V There's a bunch of display information, like the font etc under .AP.N.Resources
However, if you update the value for a field and output the pdf file. It might only display the value when the field has focus i.e is clicked on.
I haven't figured out how to fix that yet.
Solution 2
Use this to fill every fields if they are indexed.
template = PdfReader('template.pdf')
page_c = 0
while page_c < len(template.Root.Pages.Kids): #LOOP through pages
annot_c = 0
while annot_c < len(template.Root.Pages.Kids[page_c].Annots): #LOOP through fields
template.Root.Pages.Kids[page_c].Annots[annot_c].update(PdfDict(V=str(annot_c)+'-'+str(page_c)))
annot_c=annot_c+1
page_c=page_c+1
PdfWriter().write('output.pdf', template)
Solution 3
I wrote a library built upon:'pdfrw', 'pdf2image', 'Pillow', 'PyPDF2' called fillpdf (pip install fillpdf
and poppler dependency conda install -c conda-forge poppler
)
Basic usage:
from fillpdf import fillpdfs
fillpdfs.get_form_fields("blank.pdf")
# returns a dictionary of fields
# Set the returned dictionary values a save to a variable
# For radio boxes ('Off' = not filled, 'Yes' = filled)
data_dict = {
'Text2': 'Name',
'Text4': 'LastName',
'box': 'Yes',
}
fillpdfs.write_fillable_pdf('blank.pdf', 'new.pdf', data_dict)
# If you want it flattened:
fillpdfs.flatten_pdf('new.pdf', 'newflat.pdf')
More info here: https://github.com/t-houssian/fillpdf
If some fields don't fill, you can use fitz (pip install PyMuPDF
) and PyPDF2 (pip install PyPDF2
) like the following altering the points as needed:
import fitz
from PyPDF2 import PdfFileReader
file_handle = fitz.open('blank.pdf')
pdf = PdfFileReader(open('blank.pdf','rb'))
box = pdf.getPage(0).mediaBox
w = box.getWidth()
h = box.getHeight()
# For images
image_rectangle = fitz.Rect((w/2)-200,h-255,(w/2)-100,h-118)
pages = pdf.getNumPages() - 1
last_page = file_handle[pages]
last_page._wrapContents()
last_page.insertImage(image_rectangle, filename=f'image.png')
# For text
last_page.insertText(fitz.Point((w/2)-247 , h-478), 'John Smith', fontsize=14, fontname="times-bold")
file_handle.save(f'newpdf.pdf')
Comments
-
Atinesh almost 2 years
I have a
PDF form
created usingAdobe LiveCycle Designer ES 10.4
. I need to fill it usingPython
so that we can reduce manual labor. I searched the web and read some article most of them were focused aroundpdfrw
library, I tried using it and extracted some information fromPDF form
as shown belowCode
from pdfrw import PdfReader pdf = PdfReader('sample.pdf') print(pdf.keys()) print(pdf.Info) print(pdf.Root.keys()) print('PDF has {} pages'.format(len(pdf.pages)))
Output
['/Root', '/Info', '/ID', '/Size'] {'/CreationDate': "(D:20180822164509+05'30')", '/Creator': '(Adobe LiveCycle Designer ES 10.4)', '/ModDate': "(D:20180822165611+05'30')", '/Producer': '(Adobe XML Form Module Library)'} ['/AcroForm', '/MarkInfo', '/Metadata', '/Names', '/NeedsRendering', '/Pages', '/Perms', '/StructTreeRoot', '/Type'] PDF has 1 pages
I am not sure how further I can use
pdfrw
to access the fillable fields from the PDF form and fill them usingPython
is it possible. Any suggestions would be helpful.-
joelgeraci over 5 yearsForms created in Adobe LiveCycle Designer ES 10.4 come in two flavors and your task will be different based on which one you have. Designer can create either a static XFA form, which contains a normal PDF form with fields as well as an XML component for all of the logic and scripting. Alternatively, you might have a dynamic XFA form which doesn't contain a PDF form at all but relies on Adobe Reader (and a few other viewers) to render the XML into a form on the fly. My answer will depend on which type of form you have.
-
arush1836 over 5 years@joelgeraci I just have a
PDF form
I don't know how it is created. I can open it inAcrobat Reader
. -
joelgeraci over 5 yearsCan you share the file, I can identify the form type.
-
Gabriel Devillers over 5 yearsPossible duplicate of How can I auto-populate a PDF form in Django/Python?
-
Admin almost 4 years@joelgeraci In my PDF it is showing Producer as 'Adobe XML Form Module Library'. Can you please help me out with this question please. stackoverflow.com/questions/62760343/…
-
-
misterducky about 3 yearsI'm trying to conda install poppler like you mentioned, and it just keeps hanging. Did you have any trick for it? I'm trying directly in the anaconda prompt.
-
Tyler Houssian about 3 years@misterducky Interesting. There are a few different install commands on this page to try: https://anaconda.org/conda-forge/poppler. Look here as well: https://stackoverflow.com/questions/57330485/unable-to-install-poppler-on-windows-using-conda