[reportlab-users] Tables and spanning

Andy Robinson reportlab-users@reportlab.com
Tue, 28 Sep 2004 21:55:24 +0100


> I am having very serious trouble trying to get cells to span using the 
> table code in reportlab. The main problem seems to be that the code 
> cannot  span the cells that I am interested in. I am beginning to think 
> the problem is that the cell spanning screws up the indexing of the 
> table, so trying to span more than once in a given row creates 
> confusion. It also seems to try to span three columns when I ask for two.

> 
>  From what I have gathered from some posts on this list is that this 
> code is not really working properly. Has anyone used column spanning, 
> and is there any limited functionality that works ?

AFAIK it works exactly as ReportLab's staff expect it to, but nothing
like HTML, and it may probably needs better explanation.   We use it all 
the time via Report Markup Language. There is a test script 
reportlab_platypus_tables.py which (rather confusingly I admit) creates 
a file in the temp output directory 'tables.pdf'; studying this should
help.

Here's an example from that test script:
    data=  [['A', 'BBBBB', 'C', 'D', 'E'],
            ['00', '01', '02', '03', '04'],
            ['10', '11', '12', '13', '14'],
            ['20', '21', '22', '23', '24'],
            ['30', '31', '32', '33', '34']]
    sty = [
            ('ALIGN',(0,0),(-1,-1),'CENTER'),
            ('VALIGN',(0,0),(-1,-1),'TOP'),
            ('GRID',(0,0),(-1,-1),1,colors.green),
            ('BOX',(0,0),(-1,-1),2,colors.red),

            #span 'BBBB' across middle 3 cells in top row
            ('SPAN',(1,0),(3,0)),

Our grid commands come in two flavours.  The ones relating
to drawing lines refer to the 'slices between the cells',
just like Python list slicing (only 2d).  The ones relating
to 'cells themselves' such as color, font size and spanning
all count the cells starting from zero.  So the command here
            ('SPAN',(1,0),(3,0)),
says to merge the SECOND through FOURTH cells in the top row
and merges them.  This means that we draw what would have been
in the top left cell in the spanned range ('BBBB') across three
cells, and don't draw the 'C' and 'D'.

You have to understand that our commands about table ranges
are not like HTML; they live 'in the style' not 'on the cell'.
This was the easiest implementation given what we'd already built.
It also makes it easy to create a top or bottom or left row in a table
style which 'does the right thing' even though the number of columns
and rows may vary.

We have one major limitation with this model:  currently the
line-drawing commands don't take account of spanned cells. So
if you ask for a grid, it will slice across the spanned cell.
Resolving this would take a lot more computation - we'd need to
compute every edge of every cell. In real-world use this has
not been a problem as we just don't tell it to draw through 
the spans.

As always, I will reiterate that I'd welcome a contributed HTML-like
table class.  But it isn't an easy job ;-)

And if you've got a case which isn't working, send it over.

I hope this helps,

Andy Robinson