from [Link].
pagesizes import letter
from [Link] import SimpleDocTemplate, Table, TableStyle
from [Link] import colors
from [Link] import Paragraph
from [Link] 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), [Link]), # Background color for the
header row
('TEXTCOLOR', (0, 0), (-1, 0), [Link]), # 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), [Link]), # Background color for
the other rows
('GRID', (0, 0), (-1, -1), 1, [Link]) # Add a grid with black lines
])
[Link](style)
# Build the PDF with the table
elements = [table]
[Link](elements)
print(f"Table PDF created successfully: {pdf_filename}")