Drawing Tables with ReportLab: A Comprehensive Example
Tables play an integral part in visualizing and summarizing complex data in a clear, readable format. ReportLab offers a powerful object called `Table` for rendering data in table form within a PDF. By pairing this with `TableStyle`, users can customize tables to fit any styling requirements. Let's break down an example to understand the power and flexibility of ReportLab's table creation capabilities.
The first step is to know your table and cast it to a list of list. I didn't know how to do this off the top of my head, so some quick searching gave me the results! It is a common task for ReportLab users so there are many example out there. Here is my example:
Table Initialization
First, the `Table` is initialized with `budgetinlist` as its data, and column widths specified in points:
table = Table(budgetinlist, colWidths=[83,55,55,50,50,50])
`budgetinlist` should be a list of lists, where each inner list represents a row in the table.
Seen below
The `TableStyle` object holds a list of commands that define the table's style:
table.setStyle(TableStyle([ ... ]))
Each command is a tuple, where the first item is the command name and the remaining items are parameters.
- `"BACKGROUND", (0, 0), (-1, 0), "grey"`: This sets the background color of the first row (header) to grey. The `(-1, 0)` indicates the last column of the first row.
- `"TEXTCOLOR", (0, 0), (-1, 0), "white"`: Sets the text color of the first row to white.
- `"ALIGN", (0, 1), (-1, 0), "RIGHT"`: Aligns the text of the first row to the right.
- `"ALIGN", (0, 0), (-1, 0), "CENTER"`: Centers the text of the first row.
- `"FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"`: Sets the font of the first row to Helvetica-Bold.
- `"FONTSIZE", (0, 0), (-1, 0), 6.76`: Sets the font size of the first row to 6.76.
The remaining commands apply similar formatting to other parts of the table.
Drawing the Table
After creating and styling the table, you need to define the table's size and position within the canvas:
table.wrapOn(canvas, 408, 264)
table.drawOn(canvas, 10, 550)
`wrapOn()` sets the size of the table to be 408 points wide and 264 points high. `drawOn()` positions the lower-left corner of the table at x=10 and y=550 on the canvas.
Tables in ReportLab can be as simple or as intricate as your requirements dictate. This example demonstrates the power of `Table` and `TableStyle` to create and format complex tables. By altering the data and style parameters, you can build customized tables that will meet any of your data representation needs.
Below is an example of a table with grey header and footer and various styles of fonts and formats.
table = Table(budgetinlist, colWidths=[83,55,55,50,50,50])
table.setStyle(TableStyle([("BACKGROUND", (0, 0), (-1, 0), "grey"),
("TEXTCOLOR", (0, 0), (-1, 0), "white"),
("ALIGN", (0, 1), (-1, 0), "RIGHT"),
("ALIGN", (0, 0), (-1, 0), "CENTER"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("FONTSIZE", (0, 0), (-1, 0), 6.76),
("BOTTOMPADDING", (0, 0), (-1, 0), 0),
("BACKGROUND", (0, 1), (-1, -1), "white"),
("TEXTCOLOR", (0, 1), (-1, -1), "black"),
("ALIGN", (1, 1), (-1, -1), "RIGHT"),
("FONTNAME", (0, 1), (-1, -1), "Helvetica"),
("FONTSIZE", (0, 1), (-1, -1), 6.75),
("BOTTOMPADDING", (0, 1), (-1, -1), 0),
("TOPPADDING", (0, 1), (-1, -1), 0),
("RIGHTPADDING", (0, 1), (-1, -1), 0),
('FONTNAME', (0, 13), (-1, 13), 'Helvetica-Bold'),
('FONTNAME', (0, 11), (-1, 12), 'Helvetica-Bold'),
('LINEABOVE', (0, 11), (-1, 11), 1, colors.black),
('BACKGROUND', (0, 15), (-1, 15), "grey"),
("TEXTCOLOR", (0, 15), (-1, 15), "white"),
('FONTNAME', (0, 15), (-1, 15), 'Helvetica-Bold'),
('LINEABOVE', (0, 15), (-1, 15), 1, colors.black),
]))
table.wrapOn(canvas, 408, 264)
table.drawOn(canvas, 10, 550)
Comments
Post a Comment