0% found this document useful (0 votes)
104 views2 pages

Python Reportlab

The document describes a Python script that generates a PDF file containing a table with wrapped text using the ReportLab library. It includes sample data with names, ages, and descriptions, and applies specific styles for the table's appearance. The resulting PDF is named 'wrapped_table_example.pdf' and is created successfully.

Uploaded by

Suhuna Pandian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views2 pages

Python Reportlab

The document describes a Python script that generates a PDF file containing a table with wrapped text using the ReportLab library. It includes sample data with names, ages, and descriptions, and applies specific styles for the table's appearance. The resulting PDF is named 'wrapped_table_example.pdf' and is created successfully.

Uploaded by

Suhuna Pandian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

from reportlab.lib.

pagesizes import letter

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

from reportlab.lib import colors

from reportlab.platypus import Paragraph

from reportlab.lib.styles import getSampleStyleSheet

# Create a PDF file

pdf_filename = "wrapped_table_example.pdf"

pdf = SimpleDocTemplate(pdf_filename, pagesize=letter)

# Define sample data with longer text for wrapping

data = [

['Name', 'Age', 'Description'],

['Alice', '30', 'Alice is a software developer with over 10 years of


experience in the tech industry.'],

['Bob', '25', 'Bob is a data scientist working on machine learning


projects in San Francisco.'],

['Charlie', '35', 'Charlie is a product manager from Chicago who loves


solving complex problems.']

# Use styles for wrapping content

styles = getSampleStyleSheet()

wrapped_data = []

# Wrap the content in Paragraph objects for word wrapping

for row in data:

wrapped_row = [Paragraph(cell, styles["BodyText"]) for cell in row]

wrapped_data.append(wrapped_row)
# Define the column widths

col_widths = [100, 50, 300] # Set the width for each column

# Create a table with the wrapped data

table = Table(wrapped_data, colWidths=col_widths)

# Define the table style

style = TableStyle([

('BACKGROUND', (0, 0), (-1, 0), colors.grey), # Background color for the
header row

('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), # Text color for the
header row

('ALIGN', (0, 0), (-1, -1), 'CENTER'), # Align all cells to the center

('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), # Font for the header row

('BOTTOMPADDING', (0, 0), (-1, 0), 12), # Padding for the header row

('BACKGROUND', (0, 1), (-1, -1), colors.beige), # Background color for


the other rows

('GRID', (0, 0), (-1, -1), 1, colors.black) # Add a grid with black lines

])

table.setStyle(style)

# Build the PDF with the table

elements = [table]

pdf.build(elements)

print(f"Table PDF created successfully: {pdf_filename}")

You might also like