Automatically Fill PDF Forms with Python
Skills:
AI Pair Programming90%
Key Takeaways
Automatically filling PDF forms using Python, leveraging libraries such as PyPDF2 and reportlab for form manipulation and generation.
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to automatically fill PDF forms using python so let us get right into [Music] it all right so we're going to learn how to automatically fill up PDF forms using Python and this can be useful in order to automate a number of different business tasks or use cases so for example you might have a database full of customers or clients or contacts and you want to send customized invoices or contracts or proposals to them and you want to do that using PDF form so you have a PDF form with different fields and you want to fill them up dynamically with the content from the database or from a CSV file or from an EXO sheet it doesn't really matter but you can automate the process of doing that and this is what we're going to learn how to do in this video today now for this I have prepared a very simple uh template PDF file I can open it up here it's literally just a couple of labels and and four text boxes and one checkbox so I can show the forms here in my PDF uh reader and I can then you know manually enter some stuff like id1 uh some address and I can name a price of 500 let's say it's dollars and then I can add some date here something I don't know like like this uh and then I can accept the conditions and I can save this now and now it's stored in the template so let's say now I have a list of 1,000 customers and I don't want to do this all manually I want to do this in an automated way using python this is what we're going to learn how to do in this video today now you can either use an actual file that you want to try this on or you can create your own file uh I used this service here they're not sponsoring the video this is just uh the first result I found on Google maybe there are better tools out there uh I basically just started here with a blank document and dragged in some text Fields uh you can just craft your own PDF file like this then you can also click on them and you can see what their field name is this is important because those are the things that you're going to then Target in your application um and for the checkbox you also have this additional thing which is important the field value this is uh what you can have here once the value is checked this is going to be the value of the field this just uh in in this particular tool but of course you can also use any PDF file that contains forms so I'm going to empty all of these fields now and we're going to see how we can work with that uh PDF file in Python now for this we're going to need an external python package called Phill PDF so we're going to actually open up the terminal and we're going to type pip or pip 3 install Phill PDF and this is a very convenient package that we can use here in order to just easily uh locate the fields and then also fill them up and save uh the results into a new PDF file so what we're going to do is we're going to save first of all simple Le example I'm going to import datetime and we're going to say from Phill PDF import Phill PDFs and then what I want to do is I want to get all the form fields that I have so I can say form Fields is equal to fill pdf. getet form fields and I just have to provide the path template.pdf and what I want to have here is not the values I want to have only the keys because if I do it like that if I then print form fields uh you will see that I get a dictionary with the identifiers of the individual text boxes and the values and I only want to have the keys so I actually want to say keys like this then I only have the keys but I also want to have this as a list so I can um access individual Fields here using an index and then I have the list of identifiers again if you change them in the application um they will have a different name or in your PDF they will also have a different name but the order is already enough for us to know which one uh is which one so we have them from top to bottom and then we have the checkbox in the end it doesn't really matter what the exact names are here for us specifically now now let's say I want to fill this form up with some sample data let's say I have a customer ID which is going to be id1 let's say I have uh what was the second thing an address let's say it's my street 20 and let's say we have a price of 500 I'm using this uh I'm I'm passing this now as a text here or I'm defining this to be a text here because uh that is just going to be a texture value in the text box then we're going to say the date is equal to date time date time today and accept is going to be equal now this is important accept has to be equal to the value that is accepted in your specific checkbox now this is the one that I showed you in the tool and the online tool um basically when you click on this here that's the value here for this application now uh you will see which one it is if you try to pass an invalid one so if I just pass anything here you're going to see that this is not going to work so what we want to do now is we want to use a data dictionary to map the form field keys to the values and we do that by saying data dictionary is equal and then we can just say we have here the form Fields so we can say form Fields zero the first text box will have the customer ID and then we can continue to do that so I can just copy paste and then here I can change these I can change this to address I can change this to uh price I can change this to date and here I can change this to accept now we're going to get a problem here but we can try it so that we can see which values are accepted we can print also the data dictionary this is what it looks like in the end and this is now what we pass to the fill PDF package so we say fill PDFs do write fillable PDF and then we use as an input PDF the template PDF but as an output PDF we use some new PDF or new.pdf and the changes are in the data dictionary so this is the line of code that does all the magic now when I run this you will see this is not an option options are and then I see what is actually an option so I can actually change this to be that this is the checkbox being checked otherwise if I don't want to check it I can just say none so this also works so I can run this now and I have my new PDF file I can open it here in the file explorer and you will see that the data is in there and if I show the forms here you can also see that this is checked and accepted so this is how you can do that now let's go ahead and try to do it on a larger scale let's say I have this D CSV file I generated it with chat GPT so this is just customer id address price date accept basically the same thing we did here manually but I have a data set so I have id1 id2 ID3 then some streets here then some prices here some dates here and then also some yes no acceptance um that now I want to use as a basis for multiple invoices so how can we do that first of all of course you need to have pandas on your system so pip or pip 3 install pandas if you don't have it like this and then you want to import it so you want to say import pandas aspd and also we want to import OS uh up here so what we're going to do now instead of defining these fields manually is we're going to say the data frame is going to be equal to PD read CSV I want to read the CSV file which is data CSV and then I want to say for index Row in D F ital so I'm going to iterate over the rows and I'm going to do actually I don't want to delete all of this now so let me go back to it I'm going to say again DF equals PD read CSV data CSV I'm going to still do for index Row in DF itals but now I want to do this basically but with the values from the rows so I want to use actually here row customer ID and here I want to use row address and here I want to use row price and here row date and finally here now I want to do something else here I want to use um yes uncore X XC KY which is my checked if row accept is equal to yes else none um and that is now the assignment here I can now go ahead and do it like this the data dictionary is now that and for each row I'm going to do all this so I'm going to do here now uh OS path join and what I want to do is I want to join an invoices directory with some uh FST string here which is going to be the customer idore invoice so and of course we want to close this there you go and of course for this we need to create an invoices directory so we have an invoices directory and basically we just load each individual row we create a PDF file which is filled based on a template and the data dictionary and then we store it in invoices and then depending on the customer ID it's going to be a different file invoice PDF run this and you will see we have 30 PDFs filled up so I can go and open them up here in my file explorer and here we can open up one there you go you can see in this case it's checked and we have some data in here in this case it's uh also checked now let me just see if that is uh okay I messed this up uh they should not be checked so this is just going to be accept like this there you go run this again now one of them should not be checked I guess so this one is still checked but 29 should not be checked because it has a no there you go it's empty I can check it but it wasn't checked by default because in a data set I have for 29 I have a no so it's not checked but you can see I created all these invoices here using python based on a CSV file and this is how you can automate filling up PDF files so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye for
Original Description
In this video we learn how to automatically fill PDF forms with Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
The Python Skills AI Still Can’t Replace in 2026
Medium · Programming
AI's Next Frontier: Cost, Control, and the Compute Race
Dev.to AI
The Rise of Enterprise AI Coding Assistant Bans: Why Cloud Tools Are Being Blocked and What to Build Instead
Dev.to · ping wang
coding benchmarks are becoming production dependencies
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI