Re: Splitting tables with repeated ROWBACKGROUNDS
Axel P. Kielhorn <[email protected]>
| Newsgroups | gmane.comp.python.reportlab.user |
|---|---|
| Message-ID | <[email protected]> |
Am 21.03.2020 um 15:01 schrieb Robin Becker <[email protected]>: > > Hi Axel, > > wasn't quite sure what a double row was, but I guess you just mean that the rows are associated with each other in terms of background colour. > > I think I understand the problem after looking at your example. I thought it would be easier to test your ideas on something, instead of writing code based on my description. > So logically in your example you want the first two rows to be a specific background colour and then have the colour alternate between grey and white. Currently the ROWBACKGROUNDS argument is just a list and to accomplish what you seem to want it needs more structure eg a history of where in the sequence it is and the rendering needs to know about repeat rows etc etc. ROWBACKROUNDS is just a visual representation. Maybe this is the wrong way round: - there could be a logical description of some sort of grouping. - these groupings get different colours. > I'll have a think. Thanks. In the meantime I implemented my suggestion from yesterday and added an empty column with a span. It works in the test case. There is one minor issue: The table on page 2 starts with the wrong colour. Changing the ROWBACKGROUNDS definition fixed that. Have a nice weekend and stay safe. Axel
rl-ecm.py
(text/x-python-script, 3.3 KB)
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.platypus.tables import TableStyle
from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import mm
from reportlab.lib import colors
from reportlab.pdfgen import canvas
Data = [
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e"),
("Line1a", "Line1e"),
("Line1a", "Line1e"),
("Line2a", "Line2e"),
("Line2a", "Line2e")
]
Elements = []
# Abwechselnd 2 weiß / 2 grau
TableBaseStyleD = TableStyle([('ROWBACKGROUNDS', (0, 2), (-1, -1),
[colors.lightgrey, colors.lightgrey,
colors.white, colors.white])])
# Abwechselnd 2 weiß / 2 grau Schrift 9 pt mit Trennlinien
TableStyleLD = TableStyle(TableBaseStyleD.getCommands())
TableStyleLD.add('LINEBEFORE', (1, 0), (-2, -1), 1, colors.darkgrey)
TableStyleLD.add('BOTTOMPADDING', (0, 0), (-1, -1), 2) # 3
TableStyleLD.add('TOPPADDING', (0, 0), (-1, -1), 2) # 3
TableStyleLD.add('FONTSIZE', (0, 0), (-1, -1), 9)
TableStyleLD.add('LEADING', (0, 0), (-1, -1), 10)
# Header
bdata = [["Anfang", "Ende", ""]]
bdata.append(["", "", ""])
# Data
for line in Data:
an, en = line
bdata.append([an, en, ""])
for i in range(0, len(Data), 2):
TableStyleLD.add('SPAN', (-1, i), (-1, i+1))
bt = Table(bdata, repeatRows=2, colWidths=(None, None, 0))
bt.setStyle(TableStyleLD)
Elements.append(bt)
PAGE_WIDTH = defaultPageSize[0] # ist A4
PAGE_HEIGHT = defaultPageSize[1] # ist A4
TOP_MARGIN = 30 * mm
BOTTOM_MARGIN = 40 * mm
def myFirstPage(canvas, doc):
canvas.saveState()
canvas.setStrokeColor(colors.red)
canvas.line(250, BOTTOM_MARGIN, 250, PAGE_HEIGHT - TOP_MARGIN)
canvas.restoreState()
doc = SimpleDocTemplate("testpdf.pdf",
title="Title",
author="Author",
# keywords=PDFKeywords,
topMargin=TOP_MARGIN,
leftMargin=20 * mm,
rightMargin=20 * mm,
# showBoundary = 1,
bottomMargin=BOTTOM_MARGIN
)
doc.build(Elements, onFirstPage=myFirstPage, onLaterPages=myFirstPage,
canvasmaker=canvas.Canvas)