简体   繁体   中英

ReportLab rows with different columns

I am writing my data in pdf table, ,my first row has 4 columns and second row has 2 columns and 3rd row has 4 columns and so on

            table.setStyle(TableStyle([
                   ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                   ('BOX', (0,0), (-1,-1), 0.25, colors.grey),
                   ]))

This is the code i am using to style the table,but i am getting 4 columns for every row.I want to generate columns based on the length of the list i am passing like my list li [[[1,2,3,4][1,2][1,2,3,4][1,2,3]] in the first row i need 4 columns and in second row 2 columns and so on.how can i achieve it using tablestyle.

Explicitly draw only the borders you need. Look up "TableStyle Line Commands" in the documentation .

Edit:

Assuming your data list looks like:

data = [[0, 1, 2, 3],
        [0, 1],
        [0, 1, 2, 3],
        [0, 1, 2]]

then maybe something like this would work:

>>> lineweight = 0.25
>>> gridlines = [('GRID', (i, 0), (i, len(j)), lineweight, colors.grey) for i, j in enumerate(data)]
>>> gridlines
[('GRID', (0, 0), (0, 4), 0.25, colors.grey),
 ('GRID', (1, 0), (1, 2), 0.25, colors.grey),
 ('GRID', (2, 0), (2, 4), 0.25, colors.grey),
 ('GRID', (3, 0), (3, 3), 0.25, colors.grey)]

Hope that helps.

edit2: adds lineweight

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