代码之家  ›  专栏  ›  技术社区  ›  max

如何将整个表与Python Reportlab库对齐?

  •  7
  • max  · 技术社区  · 9 年前

    使用reportlab3.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 回复  |  直到 5 年前
        1
  •  13
  •   max    9 年前

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