简体   繁体   中英

How to add columns to some row of a table with reportlab?

I've been looking for an alternative for a few days but I can't find it.

I paste a fragment of my code in which I generate a table with the questions and answers, most rows will have a single column, but in particular cases I need to show information in more than one column in the same row (it can be in 2, 3, 4, columns etc.)

Is there any way to add columns to certain rows? o Specify the number of columns per row? Or another alternative.

Of course, thanks for your help

def answer_data(self, style):
    answers = []
    style_bodytext = style['BodyText']
    for a in self._answers:
        question = Paragraph(a['question_code'] + " - " + a['question'], style_bodytext)
        answer_paragraph = Paragraph(self.serializer_answer(a['answers']), style_bodytext)
        answers.append([
            question
        ])
        answers.append([
            answer_paragraph
        ])
        try:
            table_dependent = []
            qs = []
            aws = []
            for d in a['dependent']:
                q = Paragraph(d['question_code'] + " - " + d['question'], style_bodytext)
                ans = Paragraph(self.serializer_answer(d['answers']), style_bodytext)
                qs.append(q)
                aws.append(ans)
            table_dependent.append(qs)
            table_dependent.append(aws)
            answers = answers + table_dependent
        except KeyError:
            pass
    table = Table(answers, colWidths=18 * cm)
    table.setStyle([
        ("BOX", (0, 0), (-1, -1), 0.25, colors.black),
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
    ])
    for each in range(len(answers)):
        bg_color = colors.white
        if each % 2 == 0:
            bg_color = colors.lightgrey
        table.setStyle(TableStyle([
            ('BACKGROUND', (0, each), (-1, each), bg_color)
        ]))
    return table

You can use the SPAN table style to define col span for certain columns, but you will need to iterate your data beforehand, because you have to figure out the maximum number of columns and you must still provide empty strings for the columns that will be merged.

There's an example in the official documentation ; page 88:

data=  [['Top\nLeft', '', '02', '03', '04'],
    ['', '', '12', '13', '14'],
    ['20', '21', '22', 'Bottom\nRight', ''],
    ['30', '31', '32', '', '']]
t = Table(data,style=[
    ('GRID',(0,0),(-1,-1),0.5,colors.grey),
    ('BACKGROUND',(0,0),(1,1),colors.palegreen), 
    ('SPAN',(0,0),(1,1)),
    ('BACKGROUND',(-2,-2),(-1,-1), colors.pink),
    ('SPAN',(-2,-2),(-1,-1)),                ])

Output:

在此处输入图片说明

Docs:

TableStyle Span Commands

Our Table classes support the concept of spanning, but it isn't specified in the same way as html. The style specification

  SPAN, (sc,sr), (ec,er) 

indicates that the cells in columns sc - ec and rows sr - er should be combined into a super cell with contents determined by the cell (sc, sr). The other cells should be present, but should contain empty strings or you may get unexpected results.

Another possibility could be to use nested tables, eg you stick to a single column concept, and instead of just a Paragraph you create a table if you have more than one columm in that row. However, this has the downside that you can not have column captions over all columns.

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