最近项目用到reportlab,就学习了一下,发现reportlab关于表的坐标系网上解释以及用户手册解释都不是很详细,很难理解,自己学习理解后记录下心得
原手册这么介绍的
The commands passed to TableStyles come in three main groups which affect the table background,
draw lines, or set cell styles.
The first element of each command is its identifier, the second and third arguments determine the cell co-or
dinates of the box of cells which are affected with negative coordinates counting backwards from the limit
User Guide
Chapter 7 Tables and TableStyles
Page 84values 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). Depending on the command various extra (???) occur at indices beginning at 3 on.
每个命令的第一个元素是其标识符,第二个和第三个参数确定单元格坐标
受负坐标影响的单元格框的坐标从极限向后倒数
用户指南
第7章表格和表格样式
第84页的值与Python索引一样。 坐标以电子表格“ A1”后面的(列,行)形式给出
模型,而不是更自然的(对于数学家而言)“ RC”排序。 左上方的单元格是(0,0)右下方的单元格
是(-1,-1)。
从用户手册看,意思左上方的单元格是(0,0),右下方的单元格是(-1,-1)
你必须考虑正负坐标系是完全独立的。正极系统从左上角的[1][1]开始,并随着向下和向右数字递增。负极系统从[-1][-1]开始,随着你向上和向左移动而减少(较大的负极)。
data= [['00', '01', '02', '03', '04'],
['10', '11', '12', '13', '14'],
['20', '21', '22', '23', '24'],
['30', '31', '32', '33', '34']]
比如这个5*4的表,正极坐标系如下:
正极坐标系等同的负极坐标系如下:
所以可以这么理解在这个5*4表坐标系中,(0,0)的位置等价于(-5,-4)还等价于(0,-4)还等价于(-5,0)
实例:
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),(0,3),colors.red)]))
elements.append(t)
# write the document to disk
doc.build(elements)
结果如下:
('TEXTCOLOR',(0,0),(0,3),colors.red)]))对应上面的坐标系红色的一列就很好理解了
等价于('TEXTCOLOR',(-5,-4),(0,3),colors.red)]))
等价于('TEXTCOLOR',(0,-4),(0,3),colors.red)]))
等价于('TEXTCOLOR',(-5,0),(0,3),colors.red)]))