11

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.

2
  • My understanding is that if you don't use paragraphs, and put text in the table cells directly, then wrapping can happen automatically. Paragraphs, having wrapping logic of their own, need a fixed space in which to work. So why are you using paragraphs "so that they wrap correctly"?
    – Phil Frost
    Commented Jan 2, 2013 at 15:36
  • 1
    That wasn't my experience: I had tables going past the bounds of the page because they weren't wrapping, until I put the text into paragraphs. I got that idea from another SO question.
    – DeltaG
    Commented Jan 3, 2013 at 2:39

2 Answers 2

10

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.

10

(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. :)

1
  • 1
    That's neat. Have some rep. ;)
    – MaJoR
    Commented Feb 21, 2019 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.