Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What determines the vertical space in Reportlab tables?

I'm defining this style in the document:

styles.add(ParagraphStyle(name='Table Header', font ='Helvetica-Bold',fontSize=16, alignment=TA_CENTER))

I use this to define paragraphs for text to go into the top row of each table (so that they wrap correctly):

L2sub = [(Paragraph(L[0][0], styles['Table Header']))]

Later, when I add a table, there's also a place to define styles:

report.append(Table(data,style=[
                ('GRID',(0,0),(len(topiclist)-1,-1),0.5,colors.grey),
                ('FONT', (0,0),(len(topiclist)-1,0),'Helvetica-Bold',16),
                ('FONT', (0,1),(len(topiclist)-1,1),'Helvetica-Bold',12),
                ('ALIGN',(0,0),(-1,-1),'CENTER'),
                ('VALIGN',(0,0),(-1,-1),'MIDDLE'),
                ('SPAN',(0,0),(len(topiclist)-1,0)),
                ]))

My question is: where is the setting that defines the vertical height of the cells on the first row? I'm having some issues with text being too large for the cell and/or being set too low in the cell, but I can't pin down what's causing it or what to do to fix it. I've changed both sizes, but I can't get the cells to be anything other than all the same height. When I just put text into the cells instead of paragraphs, the table def'n worked nicely, but the paragraphs caused the problem.

like image 218
DeltaG Avatar asked Dec 19 '12 02:12

DeltaG


2 Answers

I don't believe there's a setting in TableStyle that allows you to change rowheight. That measurement is given when you're creating a new Table object:

Table(data, colwidths, rowheights)

Where colwidths and rowheights are lists of measurement values, like so:

from reportlab.lib.units import inch
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph
from reportlab.platypus import Table
from reportlab.lib import colors

# Creates a table with 2 columns, variable width
colwidths = [2.5*inch, .8*inch]

# Two rows with variable height
rowheights = [.4*inch, .2*inch]

table_style = [
    ('GRID', (0, 1), (-1, -1), 1, colors.black),
    ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
    ('ALIGN', (1, 1), (1, -1), 'RIGHT')
]

style = getSampleStyleSheet()

title_paragraph = Paragraph(
    "<font size=13><b>My Title Here</b></font>",
    style["Normal"]
)
# Just filling in the first row
data = [[title_paragraph, 'Random text string']]

# Now we can create the table with our data, and column/row measurements
table = Table(data, colwidths, rowheights)

# Another way of setting table style, using the setStyle method.
table.setStyle(tbl_style)

report.append(table)

colwidths and rowheights can be changed to whatever measurement you need to fit the content. colwidths reads left to right, and rowheights reads top to bottom.

If you know that all of your table rows are going to be the same height, you can use this nice shortcut:

rowheights = [.2*inch] * len(data)

Which gives you a list like [.2*inch, .2*inch, ...] for every row in your data variable.

like image 96
Nitzle Avatar answered Oct 11 '22 09:10

Nitzle


(Don't have enough reputation to comment the other answer)

Regarding the last shortcut, simply "ROW_HEIGHT = 5 * mm" is working. No need to multiply the row height per the number of row in the table.

ROW_HEIGHT = 5 * mm
curr_table = Table(data, COL_WIDTHS, rowHeights=ROW_HEIGH )

Saves a bit of memory. :)

like image 35
ZettaCircl Avatar answered Oct 11 '22 11:10

ZettaCircl