[ACCEPTED]-How to Repeat Table Column Headings over Page Breaks in PDF output from ReportLab-platypus

Accepted answer
Score: 22

From the documentation (yes, I know, but 11 it's sometimes hard to locate this stuff 10 in the manual):

The repeatRows argument specifies 9 the number of leading rows that should 8 be repeated when the Table is asked to split 7 itself.

So when you create the table, this 6 is one of the arguments you can pass, and 5 it will turn the first n rows into header 4 rows that repeat. You'll find this part 3 of the text on page 77, but the section 2 relating to creating a Table starts on page 1 76.


Score: 22

This is the code I developed, after following 2 Gordon's advice to reconsider using repeatRows, and 1 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 = []
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):

tableThatSplitsOverPages = Table(data, [6 * cm, 6 * cm], repeatRows=1)
tableThatSplitsOverPages.hAlign = 'LEFT'
tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black),

Score: 6

Use the repeatRows=1 when you create the 3 Table...

from reportlab.platypus import Table 

I always like to have something 2 you can cut & paste into a .py file 1 to run and test. So here it is...

import os
import pandas as pd
import numpy as np
import reportlab.platypus 
import reportlab.lib.styles
from reportlab.lib import colors
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import letter, landscape

reportoutputfilepath = os.path.join('.\\test.pdf')

pdf_file = reportlab.platypus.SimpleDocTemplate(
ts_tables = [
         ('ALIGN', (4,0), (-1,-1), 'RIGHT'),
         ('LINEBELOW', (0,0), (-1,0), 1, colors.purple),
         ('FONT', (0,0), (-1,0), 'Times-Bold'),
         ('LINEABOVE', (0,-1), (-1,-1), 1, colors.purple),
         ('FONT', (0,-1), (-1,-1), 'Times-Bold'),
         ('FONTSIZE', (0,0),(-1,-1), 8), 

df = pd.DataFrame(np.random.randint(0,1000,size=(1000, 4)), columns=list('ABCD'))
lista = [df.columns[:,].values.astype(str).tolist()] + df.values.tolist()

#Here is where you put repeatRows=1
table = reportlab.platypus.Table(lista, colWidths=(20*mm, 20*mm, 20*mm, 20*mm),repeatRows=1)
table_style = reportlab.platypus.TableStyle(ts_tables)
elements = []

# Build the PDF
print reportoutputfilepath
Score: 0

t1 = Table(lista, colWidths=220, rowHeights=20, repeatRows=1) just 1 type repeatRows=1

More Related questions