chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
Create a python script that allows me to import a prebuilt PDF form and add data points…
- User
Create a python script that allows me to import a prebuilt PDF form and add data points to it.
- Assistant
You can use the
PyPDF2orpdfrwandreportlablibraries to fill in a PDF form with prebuilt fields. For most PDF forms with AcroForm fields (fillable fields like text inputs), thePyPDF2library (or the more updated fork calledpypdf) can be used to read and write to these fields.Below is a Python script that:
- Opens an existing fillable PDF form.
- Fills it with data provided as a dictionary.
- Saves a new PDF with the filled-in data.
✅ Requirements:
Install these via pip before running:
pip install pypdf
📝 Python Script to Fill a Prebuilt PDF Form
from pypdf import PdfReader, PdfWriter from typing import Dict def fill_pdf_form(input_pdf_path: str, output_pdf_path: str, data: Dict[str, str]) -> None: """ Fill a PDF form with the specified data. :param input_pdf_path: Path to the input PDF file with form fields. :param output_pdf_path: Path to save the filled PDF file. :param data: Dictionary mapping field names to values. """ # Load the PDF reader = PdfReader(input_pdf_path) writer = PdfWriter() # Get and fill each page for page in reader.pages: writer.add_page(page) # Update form fields writer.update_page_form_field_values(writer.pages[0], data) # If the form looks blank, set this to True to flatten (make form fields part of the content) writer.flatten() # Optional, makes the data uneditable # Write the filled PDF with open(output_pdf_path, "wb") as output_file: writer.write(output_file) print(f"PDF form filled and saved to: {output_pdf_path}") if __name__ == "__main__": # Example paths (update these to actual file paths) input_pdf = "template_form.pdf" output_pdf = "filled_form.pdf" # Example form field data form_data = { "full_name": "John Doe", "email": "john.doe@example.com", "date": "2025-07-08", "address": "123 Main Street", # Add other fields as needed } fill_pdf_form(input_pdf, output_pdf, form_data)
🔍 How to Get PDF Field Names:
If you're unsure what the form field names are, add this snippet to list the names:
def list_pdf_fields(pdf_path: str): reader = PdfReader(pdf_path) fields = reader.get_form_text_fields() for field, value in fields.items(): print(f"Field: {field}, Current Value: {value}") # Example usage list_pdf_fields("template_form.pdf")
📌 Notes:
- This script assumes the form has AcroForm fields.
- If your PDF doesn’t use digital forms (like scanned or image-based forms), you’ll need to use OCR or writing directly onto the page with
reportlabinstead. - You don’t need to parse the whole file; just update the first page — most forms have all fields on page 1. For multi-page forms, loop over all pages.
Let me know if you'd like to sign a PDF, add checkboxes, or overlay text onto non-form PDFs!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.