Using ReportLab Table to Create Custom PDF Greeting Cards in Python
This article demonstrates how to use Python's ReportLab library, specifically the Table and TableStyle classes, to programmatically generate PDF documents such as custom greeting cards, covering basic Table usage, styling options, and step-by-step code examples for creating and customizing PDF layouts.
ReportLab is a powerful Python library for generating PDFs. By leveraging its Table class, developers can easily create complex layouts such as cards, flowcharts, and reports without manually drawing each element.
The basic Table constructor looks like this:
Table(data, colWidths=None, rowHeights=None, style=None, splitByRow=1,
repeatRows=0, repeatCols=0, rowSplitRange=None, spaceBefore=None,
spaceAfter=None, cornerRadii=None)Only the data argument (a list of lists) is required; the other parameters control dimensions and styling.
Styling is applied via TableStyle . For example, adding a green line above the first row can be done with:
('LINEABOVE', (0,0), (-1,0), 2, colors.green)Below is a minimal script ( table.py ) that creates a simple 2×4 table and draws a green line above the first row:
# table.py
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
def table_paragraph():
doc = SimpleDocTemplate("table.pdf", pagesize=letter)
story = []
data = [[1,2,3,4],[5,6,7,8]]
tblstyle = TableStyle([
('LINEABOVE', (0,0), (-1,0), 2, colors.green),
])
tbl = Table(data)
tbl.setStyle(tblstyle)
story.append(tbl)
doc.build(story)
if __name__ == '__main__':
table_paragraph()Running the script generates table.pdf , where the top row is highlighted with a green horizontal line.
Beyond simple tables, TableStyle supports many commands such as SPAN (merge cells), ALIGN (horizontal alignment), and VALIGN (vertical alignment). These allow fine‑grained control over cell appearance.
To illustrate a real‑world use case, the article provides a complete example ( table_paragraph.py ) that builds a personalized greeting card. It creates a 6‑row, 2‑column table, inserts an image, adds styled text, and applies alignment, borders, and background colors:
# table_paragraph.py
from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Image, Paragraph
def table_paragraph(name):
doc = SimpleDocTemplate("table_paragraph.pdf", pagesize=letter)
story = []
styles = getSampleStyleSheet()
pheader = f'
To {name}:
'
p0 = Paragraph(pheader, styles['Normal'])
im = Image('best_wished.jpeg')
im.drawHeight = 5 * inch * im.drawHeight / im.drawWidth
im.drawWidth = 2.5 * inch
colwidth = 3 * inch
data = [[im, p0], ['', ''], ['', ''], ['', ''], ['', 'Best'], ['', 'Wishes!']]
tblstyle = TableStyle([
('ALIGN', (-1,4), (-1,4), 'LEFT'),
('ALIGN', (-1,-1), (-1,-1), 'RIGHT'),
('TEXTCOLOR', (-1,-2), (-1,-1), colors.black),
('BOX', (0,0), (-1,-1), 0.5, colors.black),
('LINEABOVE', (-1,-4), (-1,-2), 2, colors.black),
('SPAN', (0,0), (0,-1)),
('VALIGN', (-1,-1), (-1,-1), 'TOP'),
])
tbl = Table(data, colWidths=colwidth)
tbl.setStyle(tblstyle)
story.append(tbl)
doc.build(story)
if __name__ == '__main__':
name = 'Lily'
table_paragraph(name)Executing the script produces a PDF greeting card with the recipient’s name and a decorative image. The article also shows how to adjust the layout by adding an extra empty column and tweaking column widths, providing an updated code snippet that demonstrates these refinements.
Overall, the tutorial equips readers with the knowledge to use ReportLab’s Table and TableStyle for a variety of PDF generation tasks, from simple tables to fully styled greeting cards, and encourages further experimentation such as creating flowcharts or document templates.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.