What is ReportLab Platypus With Example?

ReportLab Platypus is a subpackage or module within the ReportLab library for Python that provides a high-level API for document layout and generation in addition to the lower-level capabilities provided by the Canvas module. Platypus simplifies the creation of complex documents like PDFs by allowing you to define document structure in a more abstract and structured way.

The name “Platypus” is a nod to the platypus, an animal known for its unique combination of features. Similarly, Platypus combines various ReportLab components to allow you to create rich and diverse documents.

Here are some key features and concepts related to ReportLab Platypus:

  1. Document Flow: Platypus allows you to define a document’s flow and structure using a series of elements like paragraphs, tables, and graphics. You can add elements one after another to create a structured document.
  2. Elements: Platypus provides a variety of pre-built elements that you can use to structure your documents. Some common elements include Paragraph, Spacer, Table, Image, and more. Each element has its own parameters and attributes that you can customize.
  3. Styles: You can define and apply styles to elements, which makes it easy to maintain consistent formatting throughout your document. Styles include attributes like font, alignment, colors, and more.
  4. Page Templates: Platypus supports the creation of page templates, allowing you to define the layout of headers, footers, and content areas for different pages of your document.
  5. Flowables: Flowables are objects that can be added to the document flow, and they automatically flow from one page to the next if they exceed the available space on a page. This makes it easier to handle multi-page documents.
  6. Table of Contents (TOC): Platypus supports the creation of automatic table of contents based on the document structure.
  7. Lists: You can create lists, both numbered and bulleted, with customizable formatting.
  8. Custom Elements: While Platypus provides a range of built-in elements, you can also create your own custom elements by subclassing existing elements or creating entirely new ones.

Using ReportLab Platypus, you can create professional-looking documents with complex layouts and structures, such as reports, invoices, catalogs, and more. It abstracts many of the low-level PDF generation details, making it more straightforward to design and create documents compared to using the Canvas module directly.

Reportlab Platypus Example

Here’s a basic example of how to use ReportLab Platypus to create a simple PDF document with text and a table:

from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors

# Create a PDF document
doc = SimpleDocTemplate("platypus_example.pdf", pagesize=letter)

# Create a story (a list of document elements)
story = []

# Define a style sheet for formatting
styles = getSampleStyleSheet()

# Add a title to the document
title = Paragraph("ReportLab Platypus Example", styles['Title'])
story.append(title)

# Add some paragraphs of text
text = "ReportLab Platypus is a powerful Python library for creating PDF documents."
p = Paragraph(text, styles['Normal'])
story.append(p)

# Create a table
data = [
    ["Name", "Age", "Country"],
    ["Alice", "28", "USA"],
    ["Bob", "32", "Canada"],
    ["Charlie", "24", "UK"],
]

table = Table(data, colWidths=[150, 50, 100])
table.setStyle(TableStyle([
    ('BACKGROUND', (0, 0), (-1, 0), colors.grey),
    ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
    ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
    ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
    ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
    ('BACKGROUND', (0, 1), (-1, -1), colors.beige),
    ('GRID', (0, 0), (-1, -1), 1, colors.black)
]))

story.append(table)

# Build the PDF document
doc.build(story)
Code language: Python (python)

In this example:

  1. We import the necessary modules from ReportLab and define a PDF document with SimpleDocTemplate.
  2. We create a list called story to hold the elements of our document.
  3. We define a style sheet using getSampleStyleSheet() to format our document elements.
  4. We add a title, a paragraph of text, and a table to the story list. Each element is created with specified styles.
  5. For the table, we format the header row differently from the data rows using TableStyle.
  6. Finally, we build the PDF document with doc.build(story).

Running this script will generate a PDF file named “platypus_example.pdf” with a title, some text, and a table. You can further customize the content, styles, and layout to suit your specific needs.

Read More;

    by
  • Muhammad Nabil

    I am a skilled and experienced Python developer with a huge passion for programming and a keen eye for details. I earned a Bachelor's degree in Computer Engineering in 2019 from the Modern Academy for Engineering and Technology. I am passionate about helping programmers write better Python code, and I am confident that I can make a significant contribution to any team. I am also a creative thinker who can come up with new and innovative ways to improve the efficiency and readability of code. My specialization includes Python, Django, SQL, Apache NiFi, Apache Hadoop, AWS, and Linux (CentOS and Ubuntu). Besides my passion for Python, I am a solo traveler who loves Pink Floyd, online video games, and Italian pizza.

Leave a Comment