How to Repeat Table Column Headings over Page Breaks in PDF output from ReportLab

前端 未结 5 1844
死守一世寂寞
死守一世寂寞 2021-02-07 06:42

I\'m using ReportLab to write tables in PDF documents and am very pleased with the results (despite not having a total grasp on flowables just yet).

However, I have not

5条回答
  •  盖世英雄少女心
    2021-02-07 07:16

    This is the code I developed, after following Gordon's advice to reconsider using repeatRows, and it works!

    from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Frame, Spacer
    from reportlab.lib import colors
    from reportlab.lib.units import cm
    from reportlab.lib.pagesizes import A3, A4, landscape, portrait
    from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
    from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
    from reportlab.pdfgen import canvas
    
    pdfReportPages = "C:\\Temp\\test.pdf"
    doc = SimpleDocTemplate(pdfReportPages, pagesize=A4)
    
    # container for the "Flowable" objects
    elements = []
    styles=getSampleStyleSheet()
    styleN = styles["Normal"]
    
    # Make heading for each column and start data list
    column1Heading = "COLUMN ONE HEADING"
    column2Heading = "COLUMN TWO HEADING"
    # Assemble data for each column using simple loop to append it into data list
    data = [[column1Heading,column2Heading]]
    for i in range(1,100):
        data.append([str(i),str(i)])
    
    tableThatSplitsOverPages = Table(data, [6 * cm, 6 * cm], repeatRows=1)
    tableThatSplitsOverPages.hAlign = 'LEFT'
    tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black),
                           ('VALIGN',(0,0),(-1,-1),'TOP'),
                           ('LINEBELOW',(0,0),(-1,-1),1,colors.black),
                           ('BOX',(0,0),(-1,-1),1,colors.black),
                           ('BOX',(0,0),(0,-1),1,colors.black)])
    tblStyle.add('BACKGROUND',(0,0),(1,0),colors.lightblue)
    tblStyle.add('BACKGROUND',(0,1),(-1,-1),colors.white)
    tableThatSplitsOverPages.setStyle(tblStyle)
    elements.append(tableThatSplitsOverPages)
    
    doc.build(elements)
    

提交回复
热议问题