[英]How do I continue a for loop without going to the next index? - Python
[英]How do I continue a content to a next page in Reportlabs - Python
我正在制作一張桌子,根據收到的數據,桌子可以是小桌子還是大桌子。
雖然我提供了一個龐大的數據集,但我注意到雖然表格正在制作但我的所有內容都不存在,因為它只占用了1頁。
所以,我的問題是如何在不使用showpage()
情況下將內容繼續到Reportlabs中的showpage()
,因為我無法知道何時點擊showpage
或者何時不能,因為內容是動態生成的?
碼
def plot_table(pie_labels, pie_data, city_devices):
styles = getSampleStyleSheet()
styleN = styles["BodyText"]
styleN.alignment = TA_LEFT
styleBH = styles["Normal"]
styleBH.alignment = TA_CENTER
city_name = Paragraph('''<b>City Name</b>''', styleBH)
meter_name = Paragraph('''<b>Meter Name</b>''', styleBH)
consumption = Paragraph('''<b>Total Consumption</b>''', styleBH)
data= [[city_name, meter_name, consumption]]
# Texts
for label,record,device in zip(pie_labels,pie_data,city_devices):
label = Paragraph(label, styleN)
record = Paragraph(str(record), styleN)
device_list = ""
for d in device:
device_list += str(d) + ", "
device = Paragraph(device_list, styleN)
data.append([label, device, record])
table = Table(data, colWidths=[5.05 * cm, 5.7 * cm, 3* cm ])
table.setStyle(TableStyle([('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
]))
return table
table = plot_table(pie_labels, pie_data, city_devices)
table.wrapOn(the_canvas, width, height)
table.drawOn(the_canvas, *coord(2, 59.6, cm))
我建議使用reportlab的更高級原語,即文檔模板,框架和flowables。 這樣,你就可以獲得“免費”。 相關問題的一個例子
使用table.split()
:
from reportlab.lib.pagesizes import A4 # im using A4
width, height = A4
table_pieces = table.split(width, height)
for table_piece in table_pieces:
table_piece.drawOn(the_canvas, *coordinates)
the_canvas.show_page()
the_canvas.save()
告訴我它是否有幫助:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.