Python Forum

Full Version: Python Reportlab Wordwrap Table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a generated list in python, for having a nice layout i need a wordwrap in it. Because of beeing a list in a list I am not able to use Paragraphe () (or maybe someone know how to - i have not been able to write a functionting code)

So here the question: how can i wordwrap the text in mycells so the table fits to the page and all the text can be seen? Or can ia maybe set the width of the table so the text wordwraps itself ?

here is my code (in short):

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
########################################################################

def test():
    doc = BaseDocTemplate(
        "question.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', '24'],
        ]

t = LongTable(data)

tableStyle = [
    ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
    ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
]
t.setStyle(TableStyle(tableStyle))
elements.append(t)

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


frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height - 2 * cm, id='normal')
template = PageTemplate(id='longtable', frames=frame)
doc.addPageTemplates([template])


doc.build(elements)


if __name__ == '__main__':
    test()`