How to split ReportLab table on a PDF page (side by side)?

The code below creates a good test pattern with 99 lines of data and a title that repeats every time the page breaks. The table is quite narrow, so I'm trying to figure out how to make it split so that it has lines 1-37 on the left side of the first page, lines 38-74 on the right side of the first page and lines 75-99 on the left side of the second page. I called this "table splitting per page", but there may be a better name for what I'm trying to do. Hope I described it exactly.

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 = "COL ONE" column2Heading = "COL TWO" # 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(["Col 1 Row " + str(i),"Col 2 Row " + str(i)]) tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * 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) 
+1
source share
1 answer

You will need to use PageTemplates to do this by creating a PageTemplate page with multiple frames that allows you to specify areas of content for drawing the document on the page. Unfortunately, this means giving up on SimpleDocTemplate and instead using BaseDocTemplate and providing your own PageTemplates (as well as other things if you want them).

+2
source

All Articles