在Reportlabs—Python中,如何将内容延续到下一页

时间:2022-05-19 21:08:04

I'm making a table, where the table can be either small or large depending upon the data being received.

我正在制作一个表,根据接收到的数据,这个表可以是小的也可以是大的。

While I was providing a huge data set, I noticed that although the table is being made but my all content is not there, since it occupies only 1 page for that.

当我提供一个庞大的数据集时,我注意到虽然正在制作表,但是我的所有内容都不在那里,因为它只占用了一页。

So, my question is How do I continue a content to a next page in Reportlabs without using showpage() , since I wont be able to know when to hit showpage or when not, because the content is being dynamically generated?

所以,我的问题是,如果不使用showpage(),如何将内容继续到Reportlabs的下一个页面,因为我无法知道何时按showpage,何时不按,因为内容正在动态生成?

Code

代码

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))

2 个解决方案

#1


2  

I'd advice using the more high-level primitives of reportlab, that is, document templates, frames and flowables. That way, you get splitting for "free". An example from the related questions

我建议使用reportlab的更高级的原语,即文档模板、框架和可移动性。这样一来,你就可以“免费”地分手了。相关问题中的一个例子

#2


0  

Use table.split():

使用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()

Tell me if it helped :)

告诉我它是否有用:

#1


2  

I'd advice using the more high-level primitives of reportlab, that is, document templates, frames and flowables. That way, you get splitting for "free". An example from the related questions

我建议使用reportlab的更高级的原语,即文档模板、框架和可移动性。这样一来,你就可以“免费”地分手了。相关问题中的一个例子

#2


0  

Use table.split():

使用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()

Tell me if it helped :)

告诉我它是否有用: