Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Table Coordinate system in Python ReportLab

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

like image 947
user2242044 Avatar asked Nov 12 '15 23:11

user2242044


1 Answers

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.

like image 93
user2242044 Avatar answered Sep 18 '22 11:09

user2242044