Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Table with rows of different heights in reportlab

I am trying to create a Invoice template using reportlab. For the line items in the invoice, I am using a Table. The first row of the table contains the headers and the subsequent rows will be the actual line items. What I basically want to achieve is that if the table contains only 1 line item (plus one row of header), the table should span the entire page.

How can that be done? I don't see any way of specifying the height of the rows individually.

Thanks in advance.

like image 288
pokiman Avatar asked Jun 08 '12 12:06

pokiman


People also ask

How will you change the height of a row in a table?

Change row height To set the row height to a specific measurement, click a cell in the row that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Row Height box, and then specify the height you want. To use the ruler, select a cell in the table, and then drag the markers on the ruler.

How do I wrap text in a table cell in Reportlab?

As mentioned, you can use 'VALIGN' to wrap text within the cells, but there is another hack if you want to further control the table elements on the canvas. rowHeights attribute. _argH for finer editing of the row heights.

How can row height and column width be adjusted in a table?

Select the row or rows that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click Row Height. In the Row height box, type the value that you want, and then click OK.


2 Answers

You can specify each row heights when creating a Table object :

rows = [["Header1", "Header2"], ["Data1", "Data2"]]
table = Table(rows, colWidths=(50*mm, 50*mm), rowHeights=(10*mm, 250*mm))

To control text alignment in table cells, you can use TableStyle.

like image 93
Julien Avatar answered Sep 18 '22 10:09

Julien


You can achieve this using TableStyles and SPAN commands. You can read more about how this works starting on page 81 of the ReportLab user manual. This will let you have cells span as many rows and columns as you want.

You can also use TableStyles to adjust things like the width and hight of each row and column, but from your description that doesn't sound like what you really wanted to do.

like image 22
G Gordon Worley III Avatar answered Sep 19 '22 10:09

G Gordon Worley III