I have a list of lists
JavaScript
x
7
1
data1 = [[('Glass',1.0,'Hardware',900,900,398826),
2
('Mirror',5.0,'Hardware',18000,300,398826),
3
('Plastic',3.0,'Hardware',200,15,398826)],
4
[('Metal',1.0,'Hardware',900,900,358947),
5
('Wood',5.0,'Hardware',18000,300,358947)]
6
]
7
and i want to create a different table per list: 1 list into 1 table per page, just like this:
how do i separate the list into different tables? and have them on different pages using reportlab? assuming that the number of the lists is not always 2 because the data came from a database?
Advertisement
Answer
Simply use for
-loop to work with every list separatelly
JavaScript
1
3
1
for data in data1:
2
table = Table(data)
3
Minimal working code
JavaScript
1
36
36
1
from reportlab.lib import colors
2
#from reportlab.lib.pagesizes import letter, A4
3
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, PageBreak
4
5
all_data = [
6
[
7
('Glass',1.0,'Hardware',900,900,398826),
8
('Mirror',5.0,'Hardware',18000,300,398826),
9
('Plastic',3.0,'Hardware',200,15,398826)
10
],
11
[
12
('Metal',1.0,'Hardware',900,900,358947),
13
('Wood',5.0,'Hardware',18000,300,358947)
14
]
15
]
16
17
doc = SimpleDocTemplate("output.pdf")
18
19
table_style = TableStyle([
20
#('BACKGROUND', (1,1), (-2,-2), colors.green),
21
#('TEXTCOLOR', (0,0), (1,-1), colors.red),
22
('BOX', (0,0), (-1,-1), 0.45, colors.black),
23
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.blue),
24
])
25
26
elements = []
27
28
for data in all_data:
29
table = Table(data, style=table_style)
30
#table.setStyle(table_style)
31
elements.append(table)
32
elements.append(PageBreak())
33
34
# write the document to disk
35
doc.build(elements)
36