How to make a table with rows longer than one page in raportlab

Lea Kaminski <[email protected]>
Newsgroups gmane.comp.python.reportlab.user
Message-ID <AM0PR09MB2484A632694AA07968268FA6DE8F0@AM0PR09MB2484.eurprd09.prod.outlook.com>
Hi guys,

thank you a million times for the last answer. They worked very well. Now i got a new porblem.
I have a table, where the rows are somethimes longer than one page, and  then the programm crashes. I found some kind of solution on stack overflow (https://stackoverflow.com/questions/3297115/python-reportlab-rml-how-to-split-table-row-on-two-pages)
, which i am not able do integrate in my code the right way.

I would be really glad if someone of you know a solution how i could fix the problem.

Below the code:

from reportlab.lib.pagesizes import A4
from reportlab.lib.pagesizes import letter, cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import LongTable, TableStyle, BaseDocTemplate,Frame, PageTemplate
from reportlab.lib import colors
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.utils import simpleSplit
from reportlab.pdfbase.pdfmetrics import stringWidth

def reprFrame(frame):
    _dict = vars(frame)
    for key in sorted(list(_dict.keys())):
        print(key, ": ", _dict[key])

def test():
    doc = BaseDocTemplate(
        "question_neu.pdf",
        pagesize=A4,
        rightMargin=72,
        leftMargin=72,
        topMargin=50,
        bottomMargin=80,
        showBoundary=False)

    elements = []
    data = [['A', 'B', 'C', 'dddddddddddd', 'D'],
        ['00', '0dddddddddddddddddddddddddddddddddddd1', '02', 'fff', '04'],
        ['10', '11', '12', 'dfg', '14'],
        ['20', '21', '22', ' ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd lea hier un dda ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 dd dddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd  ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd vddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddd ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23 ddddddddddddddddddddddddddddddddddddddddddddddddddddddd23', '24'],
        ]



    tableStyle = [
        ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
        ]


    styles = getSampleStyleSheet()
    styleN = styles['Normal']


    fontName = 'Tahoma' ##This is the stuff from stack overflow
    fontSize = 16   ##This is the stuff from stack overflow
    maxWidth = 20   ##This is the stuff from stack overflow
    styleN.wordWrap = 'CJK'

    data2 = [[Paragraph(cell, styleN) for cell in row] for row in data]

    frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')
    lines = simpleSplit(data, fontName, fontSize, maxWidth) ##This is the stuff from stack overflow

    reprFrame(frame)

    colwidths = [frame._width/5. for i in range(5)]

    t = LongTable(data2, colWidths=colwidths)
    t.setStyle(TableStyle(tableStyle))
    elements.append(t)

    template = PageTemplate(id='longtable', frames=frame)
    doc.addPageTemplates([template])
    doc.build(elements)


if __name__ == '__main__':
    test()
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.