Search code examples
pythondjangoreportlab

reportlab use for loop to create multiple tables


I need through a for loop to create more tables, I think it works but I need to change the various coordinates, how can I do?

2) Is it possible to change the width of a single row of a table? or in any case bring its text-align to the left but which starts from where the table starts?

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)

Solution

  • 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')