[reportlab-users] reportlab question

Robin Becker reportlab-users@reportlab.com
Thu, 09 Sep 2004 13:46:23 +0100


Lionel Roubeyrie wrote:

> Hello all,
> I try to plot a table on a canvas with the Table module, but it cries for an 
> unexisting method :-(
> My code :
> ####################################
> from reportlab.pdfgen import canvas
> from reportlab.platypus import Table, TableStyle
> from reportlab.lib.pagesizes import A4
> from reportlab.lib.colors import green, orange, red, black
> 
> def box(c, h, a, d):
>  ''' Presentation des valeurs de hier, aujourdhui et demain'''
>  data =  [["hier", "aujourd'hui", " demain"],
>     [str(h), str(a), str(d)]]
>  t = Table(data)
>  t.setStyle(TableStyle([('ALIGN', (0,0), (-1,-1), 'CENTER'),
>       ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
>       ('FONT',(0,0), (-1,-1), ('helvetica', 12)),
>       ('INNERGRID', (0,0), (-1,-1), 1, black),
>       ('BOX', (0,0), (-1,-1), 2, black)]))
>  t.drawOn(c, 100,100)
> 
> c=canvas.Canvas("test.pdf", pagesize=A4)
> box(c,5,6,7)
> c.showPage()
> c.save()
......
>     ecp = self._colpositions[sc:ec+2]
> AttributeError: Table instance has no attribute '_colpositions'
> 
> Argggggggggg!
> 

OK you need to call the tables wrapOn method to establish its dimensions before 
you call the drawOn method. Flowables are packed handled by some kind of layout 
engine which calls the wrap and then the draw method.

in box you need to decide how the table should be wrapped by giving it some 
space to use up.

EG
   w, h = t.wrapOn(c,400,100)

use a maximum of 400 points width and 100 height.

I recommend you look at how the test_platypus_tables.py script does tables to 
see a simpler way to lay out tables. The above looks simple, but is a bag of 
worms when more dynamic tables are being used.
-- 
Robin Becker