2 years ago

#34710

test-img

the_meter413

Auto-size nested table height (rows) with ReportLab?

Is it possible to have ReportLab auto-size each row height for sub-tables?

I've got two nested tables within a table, and each nested table is a single row. I want to have different font sizes for the contents of each nested table, but when I do that only the first table seems to auto-size vertically to fit the contents; it seems like all subsequent nested tables inherit the rowHeight attribute from the first nested table, and I can't find a way to override this. How do I get the second table to vertically auto-size to fit its contents?

Here's an image to illustrate the phenomena. I added a box around each table just for illustrative purposes. I want the table containing some bar text to be auto-sized to contain the text.

enter image description here

And here's the code I'm using to generate this:

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.platypus.tables import TableStyle

inner_table_1 = Table([["Some foo text"]],)
inner_table_2 = Table([["Some bar text"]],)
outer_table = Table([[inner_table_1], [inner_table_2]])

inner_table_1_style = TableStyle([("BOX", (0, 0), (-1, -1), 1, colors.red),])
inner_table_1.setStyle(inner_table_1_style)

inner_table_2_style = TableStyle([("FONTSIZE", (0, 0), (-1, -1), 24),("BOX", (0, 0), (-1, -1), 1, colors.red),])
inner_table_2.setStyle(inner_table_2_style)

outer_table_style = TableStyle([("BOX", (0, 0), (-1, -1), 1, colors.blue),])
outer_table.setStyle(outer_table_style)

elements = []
elements.append(outer_table)

fileName = "test.pdf"
pdf = SimpleDocTemplate(fileName)

pdf.build(elements)

python

pdf-generation

reportlab

0 Answers

Your Answer

Accepted video resources