python Reportlab, add two tables side by side in pdf

4.2k views Asked by At

I am trying to insert two tables in the pdf using python reportlab. Number of rows in the second table are less than first table.
But the horizontal position of the two tables is not matching. Some empty space coming on the second table.

How can I remove this space so that both the tables will come horizontally aligned?

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter, inch
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

doc = SimpleDocTemplate("simple_table_grid.pdf", pagesize=letter)

elements = []

data= [['00', '01', '02', '03', '04'],
   ['10', '11', '12', '13', '14'],
   ['20', '21', '22', '23', '24'],
   ['30', '31', '32', '33', '34']]

data1= [['00', '01', '02', '03', '04'],
   ['10', '11', '12', '13', '14']]

t1=Table(data,5*[0.4*inch], 4*[0.4*inch],hAlign='LEFT')

t1.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                   ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                   ('VALIGN',(0,0),(0,-1),'TOP'),
                   ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                   ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                   ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                   ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                   ]))

t2=Table(data1,5*[0.4*inch], 2*[0.4*inch],hAlign='RIGHT')

t2.setStyle(TableStyle([('ALIGN',(1,1),(-2,-2),'RIGHT'),
                   ('TEXTCOLOR',(1,1),(-2,-2),colors.red),
                   ('VALIGN',(0,0),(0,-1),'TOP'),
                   ('TEXTCOLOR',(0,0),(0,-1),colors.blue),
                   ('ALIGN',(0,-1),(-1,-1),'CENTER'),
                   ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                   ('TEXTCOLOR',(0,-1),(-1,-1),colors.green),
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.black),
                   ]))
t = [[t1,t2]]

temp = Table(t)                  

elements.append(temp)

doc.build(elements) 
2

There are 2 answers

0
Bilal Aslam On

well, there is a workaround for it. You can add your tables in another table with two columns and a single row. Something like this..

    data= [(t1, t2)]
    t = Table(data, [250, 250])
    t.setStyle(TableStyle([
               ('VALIGN',(0,0),(-1,-1),'TOP')
               ]))
0
Anthony On

To have 2 tables horizontal, i don't think it's possible to do it with reportLab. I kind had the same problem, i found a way to do resolve it. I create only one table to represent both tables, and i hide the grid between the 2, so it look like it's 2 tables. With that solution you can position the 2 tables the way you want. My setStyle() looks like that

        t.setStyle(TableStyle([
                           ('GRID', (0,0), (4,1), 0.5, colors.black),
                            ('ALIGN',(1,1),(-1,-1),'CENTER'),
                            ('BACKGROUND',(0,0),(4,0),colors.lightgrey),
                            ('BACKGROUND',(3,3),(3,5),colors.lightgrey),
                            ('BOX',(3,3),(3,5),0.5,colors.black),
                            ('BOX',(4,3),(4,5),0.5,colors.black)]))