| name | reportlab-styles |
| description | Use reportlab's pre-defined styles from getSampleStyleSheet() correctly |
ReportLab Pre-Defined Styles Reference
Overview
ReportLab's reportlab.lib.styles.getSampleStyleSheet() provides a collection of pre-defined paragraph styles. Use these existing styles rather than redefining them to avoid KeyError exceptions.
Available Pre-Defined Styles
The standard stylesheet includes these commonly-used styles:
| Style Name | Purpose |
|---|
Normal | Default body text |
Title | Document title (large, bold) |
Heading1 | Level 1 section heading |
Heading2 | Level 2 section heading |
Heading3 | Level 3 section heading |
Heading4 | Level 4 section heading |
Heading5 | Level 5 section heading |
Heading6 | Level 6 section heading |
Bullet | Bulleted list items |
Definition | Definition list terms |
Italic | Italicized text |
Code | Monospace code text |
Correct Usage Pattern
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph
styles = getSampleStyleSheet()
title = Paragraph("My Document Title", styles['Title'])
heading = Paragraph("Section Header", styles['Heading1'])
body = Paragraph("Regular text content", styles['Normal'])
Common Mistake to Avoid
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(
name='Title',
fontSize=24,
))
paragraph = Paragraph("Title Text", styles['Title'])
Best Practices
- Use existing styles as-is when they meet your needs
- Create new style names for custom styles (e.g.,
MyCustomTitle, CustomHeading)
- Base custom styles on existing ones when you need modifications:
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
styles = getSampleStyleSheet()
custom_title = ParagraphStyle(
name='CustomTitle',
parent=styles['Title'],
fontSize=28,
spaceAfter=30
)
styles.add(custom_title)
styles['Title']
styles['CustomTitle']
Quick Reference Checklist