This space is available for advertisement. Please contact us if interested.
Advertise with us
automation pdf   1   28801
Automating PDF generation using Python reportlab module

We learned how to send emails with attachmentread emails from Gmail inbox and how to send messages to Microsoft teams channel.

When you are automating the mundane tasks, there might be cases when you have to generate PDF files. It could be invoices, reports, or simple documentation. In this article, we will see how to generate PDF files using Python's reportlab module.


First, we need to install reportlab module in a virtual environment. Activate the virtual environment and run the below command.

pip install reportlab

If you are using the Jupyter notebook, you can install the module as below.

import sys
!{sys.executable} -m pip install reportlab

installing module in jupyter lab

Generating PDF:

Let's start by generating a simple PDF with a title. 

from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus import Paragraph, Spacer, Table, Image
from reportlab.lib.styles import getSampleStyleSheet

styles = getSampleStyleSheet()
report = SimpleDocTemplate("/tmp/report.pdf")
report_title = Paragraph("A Complete Inventory of My Fruit", styles["h1"])[report_title])

Once this code is executed, a PDF file will be generated at location /tmp/report.pdf. The PDF file will have a title and have a heading (h1) style on the title. method expects at least one flowable as a parameter. Flowables are sort of like chunks of a document that reportlab can arrange to make a complete report. Paragraph, Spacer, Table, and Image are examples of flowable.

Adding a table to PDF:

Let's say we have a dictionary of some items as below.

fruit = {
  "elderberries": 1,
  "figs": 1,
  "apples": 2,
  "durians": 3,
  "bananas": 5,
  "cherries": 8,
  "grapes": 13

We will create a table from this dictionary to add to the PDF file. To make a Table object, we need our data to be in a list-of-lists, sometimes called a two-dimensional array. We have our inventory of fruit in a dictionary. This is how we convert a dictionary into a list-of-lists.

table_data = []
for k, v in fruit.items():
    table_data.append([k, v])


The output of the print statement will be

[['durians', 3], ['cherries', 8], ['elderberries', 1], ['figs', 1], ['apples', 2], ['grapes', 13], ['bananas', 5]]

Now create a Table flowable and add it to report in the order we want it to appear, after the title in this case.

report_table = Table(data=table_data)[report_title, report_table])

This will generate a simple table without any styles. Let's add border and alignment to the table.

from reportlab.lib import colors

table_style = [('GRID', (0,0), (-1,-1), 1,]
report_table = Table(data=table_data, style=table_style, hAlign="LEFT")[report_title, report_table])

This will generate a PDF with the title and table.

table in pdf using reportlab python

Adding a pie chart:

We’re going to need to use the Drawing Flowable class to create a Pie chart

To add data to our Pie chart, we need two separate lists: One for data, and one for labels. Once more, we’re going to have to transform our fruit dictionary into a different shape. 

from import Drawing
from import Pie
from reportlab.lib.units import inch

report_pie = Pie(width=3*inch, height=3*inch) = []
report_pie.labels = []

for fruit_name in sorted(fruit):[fruit_name])


[2, 5, 8, 3, 1, 1, 13]
['apples', 'bananas', 'cherries', 'durians', 'elderberries', 'figs', 'grapes']

The Pie object isn’t Flowable, but it can be placed inside of a Flowable DrawingNow add this chart to the report.

report_chart = Drawing()
report_chart.add(report_pie)[report_title, report_table, report_chart])

PDF will look like something as below after executing this code.

pie chart add in report using reportlab python

We will cover more points regarding generating PDF in upcoming articles.

- Here is a list of some awesome python books
Host your Django Application for free on PythonAnyWhere.
- If you want full control of your application and server, you should consider 
- Create a
 DigitalOcean account with this link and get $100 credits.

automation pdf   1   28801

Related Articles:
Automating Facebook page posts using python script
posting on facebook page using python script, automating the facebook page posts, python code to post on facebook page, create facebook page post using python script...
How to display PDF in browser in Django instead of downloading it
display pdf in browser in Django, PDF response in Django, How to display PDF response in Django, Generating PDF from HTML template in Django, Download PDF in Django, Inline pdf display in Django, In browser PDF display in Django, Showing employee payslip in the browser using Django...
Generating and Returning PDF as response in Django
Creating PDF documents in Python and Django, Returning PDF as response in Django, Generating PDF from HTML template in Django, Using pdfkit and wkhtmltopdf in python and django...
Sending email with attachments using Python built-in email module
Sending email with attachments using Python built-in email module, adding image as attachment in email while sending using Python, Automating email sending process using Python, Automating email attachment using python...

1 thought on 'Automating Pdf Generation Using Python Reportlab Module'
Spencerstok@Gmail.Com :
Hi there, I'm facing issue in account activation. I have received the email but when i click on confirmation link it's redirecting me on 404 page.

Leave a comment:

*All Fields are mandatory. **Email Id will not be published publicly.

DigitalOcean Referral Badge

Get a .COM for just $5.98!

© 2021-2022 Python Circle   Contact   Sponsor   Archive   Sitemap   Partner Sites: PythonSnippets.Dev  99Dev.Tools