Working with PDF files in Python
Hi,
I like to introduce the you to “Working with PDF files in Python” using a library called
ReportLab.
Reportlab is a pdf engine used by some programmers to generate pdf programmatically. Some of the ICT organisations that utilise reportlab include: Newcastle university, Wikipedia, HP, NASA etc. You can check this link to see who uses it and how they use it:
www.reportlab.com/casestudies
The other time I wrote a post on
How To Create PDFs with Python, then I noticed that the basics of using the library (ReporLab) wasn't detailed.
So, am using the post to discuss the: Basic of ReporLab - A Python PDF Library.
I will go over the basic concepts of using the package and then use it to solve a real world problem. To follow along, you need to install reportlab on your PC. Install it by running:
pip install reportlab
To test your installation, run:
import reportlab. If no error was returned, then your installation was successful.
Let me start by assuming that at least everyone reading this knows what a
pdf file is?
Now it is good to know that, to generate pdf files with reportlab, u will have to import it's packages. And most of the time the packages are located at:
C:\Python27\Lib\site-packages\reportlab
A package in Python is just a folder containing scripts.
The most important package to import is the "pdfgen". So the import statement will look like this:
from reportlab.pdfgen import canvas
That is to say: from the folders (reportlab/pdfgen) import the canvas object. The Canvas object has methods to manipulate ur pdf such as:
drawString,
drawLine,
to save your file etc.
Now let's create our first pdf file by using the canvas object and it's methods.
Create a .py file and save the code below in it:-
from reportlab.pdfgen import canvas
c = canvas.Canvas("filename.pdf")
c.drawString(100,750,"My First pdf file!")
c.save()
Now, if you check the location where you saved your .py script, you should see your generated pdf file with the name "
filename.pdf" containing the string "My First pdf file!".
The string is currently written at the coordinate position of: 100,750 (x, y). Origin of the coordinate (0,0) is at bottom left corner.
Play around with the coordinate to get familiar with it.
Note: that to add string to our pdf we used the
drawString() method. Another similar method to the
drawString() is the
drawCentredString() which centralize our string.
What we did above is just a one page pdf file, with every other settings as default.
In the next section we will discuss how to:-
1) Add Multiple pages to our pdf file
2) Add Font style and size to the string
3) Add image to our pdf file
4) Draw Line within our pdf file