简体   繁体   中英

How to align a entire table with the Python Reportlab Library?

Using reportlab 3.1.44 I'm trying to align the table to the left (the whole table on the page, not the cells). Here is my code:

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) 

It still remains aligned to center. Any ideas how to fix this?

Apparently the TableStyle approach does not work. Here is how I got it working:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM