【问题标题】:How to align a entire table with the Python Reportlab Library?如何将整个表格与 Python Reportlab 库对齐?
【发布时间】:2015-12-04 11:39:15
【问题描述】:

使用 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) 

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

【问题讨论】:

    标签: python report centering reportlab


    【解决方案1】:

    显然 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')
    

    【讨论】:

    • 还有t.hAlign = 'LEFT' 而不是t.alignment = TA_LEFT。几乎和你写的一样
    猜你喜欢
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 2014-02-14
    相关资源
    最近更新 更多