To create a two-column layout using Python's ReportLab, you can utilize either the Table approach (for simpler content) or the Frame and BaseDocTemplate approach (for multi-page flowing text). Below are both methods:
Method 1: Using a Table (Simple Layout)
Ideal for shorter content that fits on a single page.
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle
# Create a PDF document
doc = SimpleDocTemplate("two_columns_table.pdf", pagesize=letter)
styles = getSampleStyleSheet()
story = []
# Sample text for columns
text_col1 = "Lorem ipsum... " * 500 # Replace with your content
text_col2 = "Dolor sit amet... " * 500 # Replace with your content
# Create Paragraphs for each column
col1_paragraphs = [Paragraph(text_col1, styles['Normal'])]
col2_paragraphs = [Paragraph(text_col2, styles['Normal'])]
# Create a 1x2 table (one row, two columns)
table = Table([col1_paragraphs, col2_paragraphs], colWidths=[2.75*inch, 2.75*inch])
# Style the table (remove borders/padding)
table.setStyle(TableStyle([
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('LEFTPADDING', (0, 0), (-1, -1), 0),
('RIGHTPADDING', (0, 0), (-1, -1), 0),
('BOTTOMPADDING', (0, 0), (-1, -1), 0),
]))
story.append(table)
doc.build(story)
Method 2: Using Frames & BaseDocTemplate (Multi-Page Flowing Text)
Best for long text spanning multiple pages.
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import BaseDocTemplate, Frame, Paragraph, NextPageTemplate, PageTemplate
# Custom Document Template
class TwoColumnDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kwargs):
BaseDocTemplate.__init__(self, filename, **kwargs)
# Define two frames (columns) on the same page
frame1 = Frame(self.leftMargin, self.bottomMargin, self.width/2 - 6, self.height, id='col1')
frame2 = Frame(self.leftMargin + self.width/2 + 6, self.bottomMargin, self.width/2 - 6, self.height, id='col2')
template = PageTemplate(id='TwoColumns', frames=[frame1, frame2])
self.addPageTemplates(template)
# Create document with two columns
doc = TwoColumnDocTemplate("two_columns_flowing.pdf", pagesize=letter)
styles = getSampleStyleSheet()
story = []
# Sample content (replace with your text)
long_text = "Lorem ipsum... " * 1000 # Long text for both columns
paragraphs = [Paragraph(long_text, styles['Normal']) for _ in range(50)] # Many paragraphs
# Add content to flow across columns/pages
story.extend(paragraphs)
doc.build(story)
Key Notes:
-
Table Method:
- Simple for small content.
- Entire content must fit on one page (or manually handle splits).
- Adjust
colWidths to control column width.
-
Frame Method:
- Automatically flows text across columns and pages.
- Uses
BaseDocTemplate for custom layouts.
- Text flows from the end of column 1 to the top of column 2, then to the next page.
-
Styling:
- Use
getSampleStyleSheet() for predefined styles or create custom styles.
- Adjust margins/frame positions using
leftMargin, width, etc.
Choose the method based on your content length and complexity. The Frame approach is recommended for multi-page documents with flowing text.