Practice assembling a multi-component ReportLab PDF report containing images, bar charts, line graphs, dynamic tables, shapes, and formatted text paragraphs directly inside Google Colab.
View on GitHubWe are adding five different components to our report. We created the images, charts, graphs, and paragraphs separately and imported them into our main.py script.
from chart_verticalbar_chart import d
from table_1_basic import t
from chart_linechart import line_chart
from paragraph1 import p1, p2, p3
from chart_2_circle import d_circle
We set the path with file name in our main.py file and this file finally creates the PDF document.
from pathlib import Path
# Portable cross-platform file path construction
my_path = Path.home() / 'pdf_reports' / 'my_pdf.pdf'
Change the path of the image here.
Adding image to PDF
c.drawImage('images/top2.jpg', 0.1*inch, 10*inch) # image at left side top
from chart_verticalbar_chart import d
d.wrapOn(c, 400, 400)
d.drawOn(c, 3*inch, 7.5*inch) # vertical bar chart
Table from different data sources to PDF file
from table_1_basic import t
t.wrapOn(c, width, height)
t.drawOn(c, 3.6*inch, 5.6*inch) # table at right side
from paragraph1 import p1, p2, p3
p1.wrapOn(c, 200, 200)
p1.drawOn(c, 0.6*inch, 9.4*inch) # top paragraph of text
p2.wrapOn(c, 200, 200)
p2.drawOn(c, 0.6*inch, 6.4*inch) # middle paragraph of text
p3.wrapOn(c, 200, 200)
p3.drawOn(c, 0.6*inch, 4.4*inch) # last paragraph of text
from chart_2_circle import d_circle
d_circle.wrapOn(c, 100, 100)
d_circle.drawOn(c, 4*inch, 2.4*inch) # circles drawn at right
from chart_linechart import line_chart
line_chart.wrapOn(c, 810, 200)
line_chart.drawOn(c, 0.6*inch, 0.1*inch) # line chart at bottom
from pathlib import Path
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from chart_verticalbar_chart import d
from table_1_basic import t
from chart_linechart import line_chart
from paragraph1 import p1, p2, p3
from chart_2_circle import d_circle
output_dir = Path('output')
output_dir.mkdir(exist_ok=True)
my_path = output_dir / 'my_pdf_report.pdf'
c = canvas.Canvas(str(my_path), pagesize=letter)
width, height = letter
c.drawImage('images/top2.jpg', 0.1*inch, 10*inch) # image at left side top
d.wrapOn(c, 400, 400)
d.drawOn(c, 3*inch, 7.5*inch) # vertical bar chart
t.wrapOn(c, width, height)
t.drawOn(c, 3.6*inch, 5.6*inch) # table at right side
p1.wrapOn(c, 200, 200)
p1.drawOn(c, 0.6*inch, 9.4*inch) # top paragraph of text
p2.wrapOn(c, 200, 200)
p2.drawOn(c, 0.6*inch, 6.4*inch) # middle paragraph of text
p3.wrapOn(c, 200, 200)
p3.drawOn(c, 0.6*inch, 4.4*inch) # last paragraph of text
d_circle.wrapOn(c, 100, 100)
d_circle.drawOn(c, 4*inch, 2.4*inch) # circles drawn at right
line_chart.wrapOn(c, 810, 200)
line_chart.drawOn(c, 0.6*inch, 0.1*inch) # line chart at bottom
c.save()
print(f"Report generated successfully at: {my_path}")
Combining distinct graphical, tabular, and textual elements into a single PDF report provides significant business utility:
chart_verticalbar_chart.py) are not located in the Python execution path. Ensure all sub-modules are placed in the root execution folder or properly configured as package imports.drawOn(c, x, y) coordinates conflict. Remember that ReportLab coordinate origins $(0,0)$ start at the **bottom-left** corner of the page.D:\top2.jpg) often fails across different environments. Always use relative paths or pathlib.Path to ensure cross-platform compatibility.Q: How do `wrapOn()` and `drawOn()` work together in ReportLab canvas drawing?
A: `wrapOn(canvas, width, height)` calculates the layout dimensions required for the object within a bounded area, while `drawOn(canvas, x, y)` renders the wrapped object directly onto the canvas at coordinate $(x, y)$.
Q: Can I build multi-page reports using this canvas method?
A: Q: Yes. Call c.showPage() to start a new blank page on the canvas before invoking additional drawOn() functions.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.