python reportlab - 如何对齐表

时间:2015-09-08 17:20:08

标签: python reportlab

使用reportlab 3.1.44我试图将表格左对齐(页面上的整个表格,而不是单元格)。 这是我的代码:

from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table, TableStyle 
from reportlab.lib import colors 
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT

doc = SimpleDocTemplate('sample2.pdf', showBoundary=1) 
t = Table((('','North','South','East','West'), 
('Quarter 1',100,200,300,400), 
('Quarter 2',100,400,600,800), 
('Total',300,600,900,'1,200')), 
(72,36,36,36,36), 
(24, 16,16,18) 
) 

t.setStyle( 
TableStyle([ 
('HALIGN',(0,0),(-1,-1),'LEFT'),\
('GRID', (0,0), (-1,-1), 0.25, colors.red, None, (2,2,1)), 
('BOX', (0,0), (-1,-1), 0.25, colors.blue), 
]) 
)
t.alignment = TA_LEFT
story = [t] 
doc.build(story) 

它仍然与中心对齐。任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

显然TableStyle方法不起作用。 以下是我如何使用它:

t = Table((('','North','South','East','West'), 
('Quarter 1',100,200,300,400), 
('Quarter 2',100,400,600,800), 
('Total',300,600,900,'1,200')), 
(72,36,36,36,36), 
(24, 16,16,18) 
,hAlign='LEFT')