[reportlab-users] Newbie syntax help

Tim Roberts timr at probo.com
Thu Aug 7 17:19:08 EDT 2008


Christopher Lazzery wrote:

>

> Hi everyone. I'm brand new to Python and ReportLab and I am just

> trying to find some good tutorials out there to get me started.

>

> After reading up, I tried to start working with some of the functions

> to see if I could make something simple.

>

> I was hoping someone could explain some of the parameters in this one

> function:

>

> def box(flt, center=False):

> box_style = [('BOX', (0, 0), (-1, -1), .5, colors.lightgrey)]

> if center:

> box_style += [('ALIGN', (0, 0), (-1, -1), 'CENTER')]

>

> return Table([[flt]], style=box_style)

>

> elements = []

> styles = getSampleStyleSheet()

> doc = SimpleDocTemplate('simplebox.pdf')

>

> elements.append(box(Paragraph("Some Text Goes Here",

> styles['Title'])))

>

> doc.build(elements)

>

> Now, that builds a simple string of text with a thin grey box around

> it, as I'm sure you can tell. I'm confused about the "(0, 0), (-1,-1)"

> in the box function though. From what I've read, I think those are

> coordinates, but I have no idea what they do (if they even are

> coordinates). If I change any of the numbers, the box doesn't move. It

> disappears. So, what are these parameters? Are they coordinates? I

> know this seems like a silly question, but neither the User Guide nor

> any other documentation I have found seems to explain it.

>


The general rule in Python is that negative coordinates start counting
from the end. [0] is the first of something, [-1] is the last of something.

The two tuples in the style declarations give the coordinates of two
corners of the rectangle of cells that this style should apply to. So,
(0,0) means the cell at column 0, row 0; upper left hand corner.
(-1,-1) means the cell in the right-most column, bottom-most row; lower
right hand corner. So, both of these styles apply to the whole table.
If you change the first one to (1,0),(-1,-1), it would apply to all
cells except the first column.

It is common to want a different style for the top row ( (0,0),(-1,0) ),
or the left column ( (0,0),(0,-1) ).

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the reportlab-users mailing list