5
votes

I am having trouble understanding the coordinate system for ReportLab table styles.

According to their documentation:

The first element of each command is its identifier, the second and third arguments determine the cell coordinates of the box of cells which are affected with negative coordinates counting backwards from the limit values as in Python indexing.

The coordinates are given as (column, row) which follows the spreadsheet 'A1' model, but not the more natural (for mathematicians) 'RC' ordering. The top left cell is (0, 0) the bottom right is (-1, -1)

If the bottom right is always (-1, -1), does that mean the mid rows and columns are fractional? That doesn't seem to makes sense. Given the below example, how is the green starting at position (1,1) is the coordinate system goes from 0 to -1 and how does the second coordinate system (-2,-2) offset from the origin position?

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table.pdf", pagesize=letter)
# container for the 'Flowable' objects
elements = []

data= [['00', '01', '02', '03', '04'],
       ['10', '11', '12', '13', '14'],
       ['20', '21', '22', '23', '24'],
       ['30', '31', '32', '33', '34']]
t=Table(data)
t.setStyle(TableStyle([('BACKGROUND',(1,1),(-2,-2),colors.green),
                       ('TEXTCOLOR',(0,0),(1,-1),colors.red)]))
elements.append(t)
# write the document to disk
doc.build(elements)

enter image description here

3
those tables styles are applied to cells not pixel coordinates - Joran Beasley
@JoranBeasley, I know, but my question still applies to them being table coordinates. Can you please elaborate? - user2242044
I guess I dont understand your question ... you dont ned to use negative ordering ... TABLE[-1,-1] in this case is exactly the same as TABLE[3,4] in python you can access the end of the list with -1 so TABLE[-1] is the last row TABLE[-1][-1] is simply the last column of the last row (maybe i got my row/column order switched) - Joran Beasley
So the negative one is not like subtracting one from the origin position? - user2242044
Same question here. How does the TABLE stop being rendered in green at [-2][-2] as specified in the code? - alextc

3 Answers

9
votes

I finally figured it out so posting my own answer. You have to think of negative and positive coordinate systems being completely independent of each other. The positive system starts at [1][1] in the top left corner and increments numbers as you go down and to the right. The negative system starts at [-1][-1] as decreases (larger negatives) as you go up and to the left.

In this example, ('BACKGROUND',(1,1),(-2,-2),colors.green) The two coordinates, while using different relative starting position define a the upper left and lower right corners of a box.

2
votes

I have found it is easier to think of the cells as lists rather that coordinates for this issue. Top left cell is (0,0) and the last column in this example is 4 which can also be represented as -1, just like a list. The second to last column in this example is 3 which can also be -2, etc.

I found challenging the fact that the cell references are (col, row) rather than (row, col).

2
votes

Reportlab coordinates for a table (sample)

Now imagine your table is of size (5,4), then this would be the kind of coordinates that you will need to follow!