reportlab使用for循环创建多个表

reportlab use for loop to create multiple tables

我需要通过一个for循环来创建更多的tables,我觉得可行但是我需要改变各种坐标,我该怎么做?

2) 是否可以更改 table 的单行宽度?或者在任何情况下将其文本对齐到左侧但从 table 开始的地方开始?

def testPdfView(request, id):

    #dati init
    scheda = get_object_or_404(Schede, pk = id)
    filename = 'media/pdf/' + scheda.nome_scheda + '.pdf'
    titolo =  scheda.utente.username + ' - ' + scheda.nome_scheda

    #creazione file
    doc = SimpleDocTemplate(
        filename,
        pagesize = A4,
        rightMargin = 10*mm,
        leftMargin = 10*mm,
        topMargin = 47*mm,
        bottomMargin = 10*mm
    )

    #titolo
    doc.title = titolo

    #passaggio scheda alla funzione pdfCanvas
    doc.scheda = scheda

    #table gruppi
    gruppi = DatiGruppi.objects.filter(gruppi_scheda = id)
    for gruppo in gruppi:
        table = Table([
            [str(gruppo).upper()]
        ], colWidths= 180*mm, repeatRows=1)
    
    #table style
    style = TableStyle([
        ('TEXTCOLOR', (0,0),(-1,0), colors.HexColor("#9FFC0D")),# -1 significa l'ultimo elemento
        ('FONTNAME', (0,0), (0,0), 'bulk_bold'),
        ('FONTSIZE', (0,0), (0,0), 6*mm),
        ('BOTTOMPADDING', (0,0), (-1,0), 6*mm),
        ('LINEBELOW',(0,0),(-1,0), 1, colors.HexColor("#9FFC0D")),
    ])
    table.setStyle(style)

    #table add to template
    elems = []
    elems.append(table)

    #create
    doc.build(elems, onFirstPage = pdfCanvas)
def genGroupTable(gruppo):
    groupElemTeble = None

    #tab
    titleTable = Table([
        [str(gruppo).upper(), str(gruppo.giorni_settimana).upper()]
    ], colWidths= 95*mm)

    titleTable_style = TableStyle([
        ('TEXTCOLOR', (0,0),(-1,0), colors.HexColor("#9FFC0D")),
        ('FONTNAME', (0,0), (0,0), 'bulk_bold'),
        ('ALIGN',(1,0),(-1,0),'RIGHT'),
        ('VALIGN',(0,0),(-1,0),'MIDDLE'),
        ('FONTSIZE', (0,0), (0,0), 6*mm),
        ('BOTTOMPADDING', (0,0), (0,0), 6*mm),
        ('LINEBELOW',(0,0),(-1,0), 1, colors.HexColor("#9FFC0D")),
        ('LEFTPADDING',(0,0),(-1,-1), 0*mm),
        ('RIGHTPADDING',(0,0),(-1,-1), 0*mm)
    ])
    titleTable.setStyle(titleTable_style)

    thead = Table([
        ['ESERCIZIO','SERIE','RIPETIZIONI','PESO']
    ], colWidths= 47.5*mm)

    thead_style = TableStyle([
        ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor("#19212A")),
        ('TEXTCOLOR', (0,0),(-1,0), colors.HexColor("#ffffff")),
        ('FONTNAME', (0,0), (-1,0), 'bulk_testo'),
        ('ALIGN',(0,0),(-1,0),'CENTER'),
        ('VALIGN',(0,0),(-1,0),'MIDDLE'),
        ('BOTTOMPADDING', (0,0), (-1,0), 1*mm)
    ])
    thead.setStyle(thead_style)

    exercise = []
    elementi = []
    for esercizio in gruppo.gruppo_single.all():
        exercise.append(esercizio)

    for es in range(len(exercise)):
        tbody = Table([
            [str(exercise[es]).upper(), str(exercise[es].serie).upper(), str(exercise[es].ripetizione).upper(), str(exercise[es].peso).upper()+'KG']
        ], colWidths= 47.5*mm)

        tbody_style = TableStyle([
            ('TEXTCOLOR', (0,0),(-1,-1), colors.HexColor("#ffffff")),
            ('FONTNAME', (0,0), (-1,-1), 'bulk_testo'),
            ('ALIGN',(0,0),(-1,-1),'CENTER'),
            ('BOTTOMPADDING', (0,0), (-1,-1), 1*mm),
            ('LINEBELOW',(0,0),(-1,-1), .2, colors.HexColor("#ffffff"))
        ])
        tbody.setStyle(tbody_style)

        elementi.append(tbody)

    #tab finale
    groupElemTeble = Table([
        [titleTable],
        [thead],
        [[elementi]]
    ], colWidths = 190*mm)

    groupElemTeble_style = TableStyle([
        #('BACKGROUND', (0, 0), (-1, -1), colors.HexColor("#202B38")),
        ('LEFTPADDING',(0,0),(-1,-1), 0*mm),
        ('RIGHTPADDING',(0,0),(-1,-1), 0*mm),
        ('BOTTOMPADDING',(-1,-1),(-1,-1), 5*mm)
    ])
    groupElemTeble.setStyle(groupElemTeble_style)

    return groupElemTeble


def testPdfView(request, id):

    #dati init
    scheda = get_object_or_404(Schede, pk = id)
    filename = 'media/pdf/' + scheda.nome_scheda + '.pdf'
    titolo =  scheda.utente.username + ' - ' + scheda.nome_scheda

    #creazione file
    doc = SimpleDocTemplate(
        filename,
        pagesize = A4,
        rightMargin = 10*mm,
        leftMargin = 10*mm,
        topMargin = 47*mm,
        bottomMargin = 10*mm
    )

    #titolo
    doc.title = titolo

    #passaggio scheda alla funzione pdfCanvas
    doc.scheda = scheda

    #elemento vuoto
    elems = []

    #lista gruppi
    group = []
    gruppi = DatiGruppi.objects.filter(gruppi_scheda = id)
    for gruppo in gruppi:
        group.append(gruppo)

    for gr in range(len(group)):
        main = genGroupTable(group[gr])

        elems.append(main)

    #create
    doc.multiBuild(elems, onFirstPage = header)

    #return 
    percorso = str(settings.BASE_DIR) +'/media/pdf/' + scheda.nome_scheda + '.pdf'
    return FileResponse(open(percorso, 'rb'), content_type='application/pdf')