Basic Document Structure
```python
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
# Create document
doc = SimpleDocTemplate("document.pdf", pagesize=letter)
story = [] # Container for flowable elements
# Get styles
styles = getSampleStyleSheet()
# Add content
story.append(Paragraph("Document Title", styles['Title']))
story.append(Spacer(1, 0.2*inch))
story.append(Paragraph("This is a paragraph of text.", styles['Normal']))
story.append(Paragraph("This is another paragraph.", styles['Normal']))
# Build PDF
doc.build(story)
```
Working with Paragraphs and Styles
```python
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib.enums import TA_CENTER, TA_JUSTIFY
from reportlab.lib.units import inch
from reportlab.lib import colors
doc = SimpleDocTemplate("styled_doc.pdf", pagesize=letter)
story = []
styles = getSampleStyleSheet()
# Built-in styles
story.append(Paragraph("Title Style", styles['Title']))
story.append(Paragraph("Heading 1", styles['Heading1']))
story.append(Paragraph("Heading 2", styles['Heading2']))
story.append(Paragraph("Normal paragraph text.", styles['Normal']))
story.append(Spacer(1, 0.2*inch))
# Custom style
custom_style = ParagraphStyle(
'CustomStyle',
parent=styles['Normal'],
fontSize=14,
textColor=colors.blue,
alignment=TA_CENTER,
spaceAfter=10,
)
story.append(Paragraph("Centered blue text", custom_style))
# Justified paragraph
justified_style = ParagraphStyle(
'Justified',
parent=styles['Normal'],
alignment=TA_JUSTIFY,
)
long_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " * 10
story.append(Paragraph(long_text, justified_style))
doc.build(story)
```
Creating Tables
```python
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph
from reportlab.lib.pagesizes import letter
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
doc = SimpleDocTemplate("table.pdf", pagesize=letter)
story = []
styles = getSampleStyleSheet()
story.append(Paragraph("Sales Report", styles['Title']))
# Table data
data = [
['Product', 'Q1', 'Q2', 'Q3', 'Q4'],
['Widget A', '$1,000', '$1,200', '$1,100', '$1,300'],
['Widget B', '$800', '$900', '$950', '$1,000'],
['Widget C', '$1,500', '$1,600', '$1,700', '$1,800'],
['Total', '$3,300', '$3,700', '$3,750', '$4,100'],
]
# Create table
table = Table(data)
# Style table
table.setStyle(TableStyle([
# Header row
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, 0), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('FONTSIZE', (0, 0), (-1, 0), 12),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
# Data rows
('BACKGROUND', (0, 1), (-1, -2), colors.beige),
('ALIGN', (1, 1), (-1, -1), 'RIGHT'),
('FONTNAME', (0, 1), (-1, -1), 'Helvetica'),
('FONTSIZE', (0, 1), (-1, -1), 10),
('GRID', (0, 0), (-1, -1), 1, colors.black),
# Total row
('BACKGROUND', (0, -1), (-1, -1), colors.lightgrey),
('FONTNAME', (0, -1), (-1, -1), 'Helvetica-Bold'),
]))
story.append(table)
doc.build(story)
```
Adding Charts
```python
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.graphics.shapes import Drawing
from reportlab.graphics.charts.barcharts import VerticalBarChart
from reportlab.graphics.charts.piecharts import Pie
from reportlab.lib import colors
doc = SimpleDocTemplate("charts.pdf", pagesize=letter)
story = []
styles = getSampleStyleSheet()
story.append(Paragraph("Sales Charts", styles['Title']))
# Bar chart
drawing = Drawing(400, 200)
bar_chart = VerticalBarChart()
bar_chart.x = 50
bar_chart.y = 50
bar_chart.height = 125
bar_chart.width = 300
bar_chart.data = [[100, 150, 200, 175, 225]]
bar_chart.categoryAxis.categoryNames = ['Q1', 'Q2', 'Q3', 'Q4', 'Q5']
bar_chart.bars[0].fillColor = colors.blue
drawing.add(bar_chart)
story.append(drawing)
# Pie chart
drawing2 = Drawing(400, 200)
pie = Pie()
pie.x = 150
pie.y = 50
pie.width = 100
pie.height = 100
pie.data = [30, 25, 20, 15, 10]
pie.labels = ['Product A', 'Product B', 'Product C', 'Product D', 'Other']
pie.slices.strokeWidth = 0.5
drawing2.add(pie)
story.append(drawing2)
doc.build(story)
```