Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 372
max Avatar asked Sep 08 '15 17:09

max


1 Answers

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')
like image 175
max Avatar answered Nov 15 '22 02:11

max