OpenSpace reportlab-styles
Use reportlab's pre-defined styles from getSampleStyleSheet() correctly
install
source · Clone the upstream repo
git clone https://github.com/HKUDS/OpenSpace
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/HKUDS/OpenSpace "$T" && mkdir -p ~/.claude/skills && cp -r "$T/gdpval_bench/skills/reportlab-styles" ~/.claude/skills/hkuds-openspace-reportlab-styles && rm -rf "$T"
manifest:
gdpval_bench/skills/reportlab-styles/SKILL.mdsource content
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 |
|---|---|
| Default body text |
| Document title (large, bold) |
| Level 1 section heading |
| Level 2 section heading |
| Level 3 section heading |
| Level 4 section heading |
| Level 5 section heading |
| Level 6 section heading |
| Bulleted list items |
| Definition list terms |
| Italicized text |
| Monospace code text |
Correct Usage Pattern
from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import Paragraph # Get the stylesheet styles = getSampleStyleSheet() # Use existing styles directly (CORRECT) title = Paragraph("My Document Title", styles['Title']) heading = Paragraph("Section Header", styles['Heading1']) body = Paragraph("Regular text content", styles['Normal'])
Common Mistake to Avoid
# WRONG - Don't redefine existing style names from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle styles = getSampleStyleSheet() # This will cause KeyError when you try to use 'Title' later styles.add(ParagraphStyle( name='Title', # Overwrites the pre-defined 'Title' style fontSize=24, # ... incomplete definition )) # Later code expecting the full 'Title' style will fail paragraph = Paragraph("Title Text", styles['Title']) # KeyError!
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() # Create a NEW style with a unique name custom_title = ParagraphStyle( name='CustomTitle', # Unique name, doesn't conflict parent=styles['Title'], # Base it on existing Title fontSize=28, spaceAfter=30 ) styles.add(custom_title) # Now both styles are available styles['Title'] # Original pre-defined style styles['CustomTitle'] # Your custom variant
Quick Reference Checklist
- Import
fromgetSampleStyleSheet()reportlab.lib.styles - Call
once at the startstyles = getSampleStyleSheet() - Access styles via
(case-sensitive)styles['StyleName'] - Do NOT add styles with names that already exist in the stylesheet
- Use unique names for any custom styles you create
- Common available styles:
,Normal
,Title
,Heading1-6
,Bullet
,CodeItalic