[reportlab-users] How to make a table with rows longer than one page in raportlab
Lea Kaminski
Amy_658 at hotmail.com
Sat Jan 5 05:58:35 EST 2019
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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20190105/68d8ec3c/attachment.html>
More information about the reportlab-users
mailing list