如何在 reportlab 中将列表分离到不同的表格并将表格打印到单独的页面 pdf?
how to separate list to different tables & print the tables to individual pages pdf in reportlab?
我有一个列表列表
data1 = [[('Glass',1.0,'Hardware',900,900,398826),
('Mirror',5.0,'Hardware',18000,300,398826),
('Plastic',3.0,'Hardware',200,15,398826)],
[('Metal',1.0,'Hardware',900,900,358947),
('Wood',5.0,'Hardware',18000,300,358947)]
]
我想为每个列表创建一个不同的 table:每页 1 个列表到 1 个 table,就像这样:
如何将列表分成不同的 table?并使用 reportlab 将它们放在不同的页面上?假设列表的数量并不总是 2,因为数据来自数据库?
只需使用 for
-loop 分别处理每个列表
for data in data1:
table = Table(data)
最少的工作代码
from reportlab.lib import colors
#from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, PageBreak
all_data = [
[
('Glass',1.0,'Hardware',900,900,398826),
('Mirror',5.0,'Hardware',18000,300,398826),
('Plastic',3.0,'Hardware',200,15,398826)
],
[
('Metal',1.0,'Hardware',900,900,358947),
('Wood',5.0,'Hardware',18000,300,358947)
]
]
doc = SimpleDocTemplate("output.pdf")
table_style = TableStyle([
#('BACKGROUND', (1,1), (-2,-2), colors.green),
#('TEXTCOLOR', (0,0), (1,-1), colors.red),
('BOX', (0,0), (-1,-1), 0.45, colors.black),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.blue),
])
elements = []
for data in all_data:
table = Table(data, style=table_style)
#table.setStyle(table_style)
elements.append(table)
elements.append(PageBreak())
# write the document to disk
doc.build(elements)
我有一个列表列表
data1 = [[('Glass',1.0,'Hardware',900,900,398826),
('Mirror',5.0,'Hardware',18000,300,398826),
('Plastic',3.0,'Hardware',200,15,398826)],
[('Metal',1.0,'Hardware',900,900,358947),
('Wood',5.0,'Hardware',18000,300,358947)]
]
我想为每个列表创建一个不同的 table:每页 1 个列表到 1 个 table,就像这样:
如何将列表分成不同的 table?并使用 reportlab 将它们放在不同的页面上?假设列表的数量并不总是 2,因为数据来自数据库?
只需使用 for
-loop 分别处理每个列表
for data in data1:
table = Table(data)
最少的工作代码
from reportlab.lib import colors
#from reportlab.lib.pagesizes import letter, A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, PageBreak
all_data = [
[
('Glass',1.0,'Hardware',900,900,398826),
('Mirror',5.0,'Hardware',18000,300,398826),
('Plastic',3.0,'Hardware',200,15,398826)
],
[
('Metal',1.0,'Hardware',900,900,358947),
('Wood',5.0,'Hardware',18000,300,358947)
]
]
doc = SimpleDocTemplate("output.pdf")
table_style = TableStyle([
#('BACKGROUND', (1,1), (-2,-2), colors.green),
#('TEXTCOLOR', (0,0), (1,-1), colors.red),
('BOX', (0,0), (-1,-1), 0.45, colors.black),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.blue),
])
elements = []
for data in all_data:
table = Table(data, style=table_style)
#table.setStyle(table_style)
elements.append(table)
elements.append(PageBreak())
# write the document to disk
doc.build(elements)