Splitting tables with repeated ROWBACKGROUNDS

"Axel P. Kielhorn" <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <[email protected]>
Hello!

I have some data that I want to put into a table, but it doesn’t fit into one row.
Therefore I split it into two rows and use ROWBACKGROUNDS to group the rows.

This works great until I have to split the table.
(Well reportlab splits the table for me.)

Sometimes, depending on the start position on the page, a table break will occur inside a double row.
This means the grouping on the following page is wrong.

Is there a way to influence the table break?
(Every second (or nth) row)

An alternative would be to SPAN the first column, hoping the table won’t break a SPAN.
But this means I have to track my data and add a TABLESTYLE for each double row.

Right now I simply throw my data at the table and hope for the best.

Please find included an example of what I’m doing right now.

Greetings
Axel
rl-ecm.py (text/x-python-script, 3.2 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, 0), (-1, -1),
                               [colors.white, colors.white,
                                colors.lightgrey, colors.lightgrey])])

# Abwechselnd 2 weiß / 2 grau Schrift 9 pt mit Trennlinien
TableStyleLD = TableStyle(TableBaseStyleD.getCommands())
TableStyleLD.add('LINEBEFORE', (1, 0), (-1, -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])

bt = Table(bdata, repeatRows=2)
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)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.