Fundamentals 5 min read

Generating Automated PDF Reports with Python: ReportLab, Matplotlib, and Platypus

This article demonstrates how to automate the creation of PDF reports in Python by combining ReportLab for PDF generation, Matplotlib for data visualization, and Platypus for table handling, providing step-by-step code examples for installing libraries, drawing text and images, and assembling comprehensive reports.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Generating Automated PDF Reports with Python: ReportLab, Matplotlib, and Platypus

When manual report creation becomes tedious, Python can automate the entire workflow from data processing to PDF generation.

1. ReportLab: PDF generation helper Install the library with pip install reportlab and create a simple PDF:

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
pdf = canvas.Canvas("SimpleReport.pdf", pagesize=A4)
pdf.drawString(100, 750, "This is my first Python‑generated report")
pdf.save()

2. Data visualization: make the report lively Use matplotlib to draw a chart and save it as an image:

import matplotlib.pyplot as plt
sales = [120, 150, 180, 210, 165]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
plt.figure(figsize=(8, 6))
plt.bar(months, sales)
plt.title('Sales Data')
plt.savefig('sales_chart.png')
plt.close()

3. Combine text and images in a single PDF Load the chart image and embed it together with headings:

from reportlab.lib.utils import ImageReader
pdf = canvas.Canvas("FullReport.pdf", pagesize=A4)
pdf.setFont("Helvetica-Bold", 16)
pdf.drawString(100, 750, "Q1 2024 Sales Report")
img = ImageReader('sales_chart.png')
pdf.drawImage(img, 50, 400, width=500, height=300)
pdf.setFont("Helvetica", 12)
pdf.drawString(100, 350, "The chart shows a steady increase in sales.")
pdf.save()

4. Advanced: automatic table handling The platypus module can build tables with custom styles:

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib import colors

def create_table_report():
    doc = SimpleDocTemplate("TableReport.pdf", pagesize=A4)
    data = [
        ['Month', 'Sales', 'YoY Growth'],
        ['Jan', '1.2M', '15%'],
        ['Feb', '1.5M', '25%'],
        ['Mar', '1.8M', '20%']
    ]
    table = Table(data)
    table.setStyle([
        ('GRID', (0,0), (-1,-1), 1, colors.black),
        ('BACKGROUND', (0,0), (-1,0), colors.grey)
    ])
    doc.build([table])

5. Full example The article combines all steps into a single script that generates a chart, creates a PDF with title, image, and explanatory text, and optionally adds a styled table, showing how to produce a polished, data‑driven PDF report with just a few lines of Python.

pythonAutomationData VisualizationMatplotlibPDF generationReportLab
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.