Reputation: 473
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)
i found a code on this page which will not do the wordwrap, even though it says so.
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?
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()`
Upvotes: 3
Views: 6909
Reputation: 415
I modified, and sligthly rearanged your example so that it does the right thing again, actually the cited page was doing what it promised, but missing that you have to pass colWidth argument, I hope you can go on from here:
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 reprFrame(frame):
_dict = vars(frame)
for key in sorted(list(_dict.keys())):
print(key, ": ", _dict[key])
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'],
]
tableStyle = [
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
]
styles = getSampleStyleSheet()
styleN = styles['Normal']
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')
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()
Upvotes: 5