ReportLab Table Cell Follow-Up

Previously, I documented wrestling with links inside table cells. There, I wrote:

So, for example, if you have one cell with a number and another with a long string of text, you can’t say, “Make the number cell X wide, and make the text cell Y wide.”

That’s completely wrong, and the ReportLab documentation proves it. While I did scan the docs, it’s safe to say that I didn’t actually RTFM.

You can set the same width for every table column by passing a number (the behavior I complained about):

inch = 72 # 72 points per inch
t = Table(table_data, colWidths=1*inch)

or, you can set different widths for each by passing a list of numbers:

t = Table(table_data, colWidths=[1*inch, 2*inch, 3*inch])

This well-documented feature is a boon to my application, which–until today–suffered from table cell overruns and general suckiness.

I wrote a function that measures the longest string of each column, then sets the width appropriately. If the total length of all columns exceeds that of the page (less the margins), then I have a “sacrifice” column that’s left to text-wrap as ReportLab sees fit.

While I can report that things are coming together pretty well using the open source libraries… work has agreed to pony up for an “Enterprise” license. I’ll dive into merging PDFs and popping my RML cherry next week.

I have to fix that whole “using A4 instead of 8.5×11 anachronism” layout problem first.

Stupid metric system.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s