The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Auto-fill PDF forms with Python

Auto-filling PDF forms using Python can be accomplished with the help of libraries like PyPDF2, pdfrw, or reportlab. However, for more advanced manipulation, especially for filling out form fields in a structured and user-friendly way, pikepdf or pdfFiller might be more appropriate.

Here’s an example of how to auto-fill a PDF form using pdfrw and reportlab.

1. Install the necessary libraries

First, you’ll need to install pdfrw and reportlab. You can do this via pip:

bash
pip install pdfrw reportlab

2. Create the PDF with form fields

Let’s assume you have an existing PDF form with fields. If not, you can create your own using a PDF editor that supports form creation (like Adobe Acrobat).

3. Write the Python code to fill out the form fields

Here’s how you can fill out the fields programmatically.

python
import pdfrw def fill_pdf_form(input_pdf_path, output_pdf_path, data): # Read the template PDF template_pdf = pdfrw.PdfReader(input_pdf_path) # Loop through the pages and fill the form fields for page in template_pdf.pages: annotations = page['/Annots'] if annotations: for annotation in annotations: field = annotation.get('/T') if field: field_name = field[1:-1] # Remove parentheses from the field name if field_name in data: # Set the field value from the data dictionary annotation.update(pdfrw.PdfDict(V='{}'.format(data[field_name]))) # Write the updated PDF to output pdfrw.PdfWriter(output_pdf_path, trailer=template_pdf).write() # Example data to fill the form data = { 'name': 'John Doe', 'email': 'johndoe@example.com', 'phone': '123-456-7890' } # Input and output PDF paths input_pdf = 'input_form.pdf' output_pdf = 'filled_form.pdf' # Fill the form fill_pdf_form(input_pdf, output_pdf, data)

Explanation:

  • pdfrw.PdfReader(input_pdf_path): Reads the input PDF form.

  • pdfrw.PdfWriter(output_pdf_path, trailer=template_pdf): Writes the modified PDF to the output path.

  • annotations: Each form field is considered an annotation in the PDF. These annotations are iterated through, and if the annotation’s /T field matches a key in your data dictionary, the corresponding /V (value) field is updated with the value from the dictionary.

4. Check the Output

After running this script, the filled_form.pdf will contain the values you provided in the data dictionary, filled into the corresponding form fields.

Notes:

  • The field names in the data dictionary must match exactly with the field names in the PDF form.

  • This solution works for PDF forms that use the standard AcroForm structure.

If you want to explore more advanced PDF editing (like creating form fields dynamically, or handling more complex PDF layouts), you can use libraries like pikepdf or reportlab. But for simple form filling, pdfrw is lightweight and easy to use.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About