Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I continue a content to a next page in Reportlabs - Python

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?

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))
like image 997
PythonEnthusiast Avatar asked Dec 06 '13 04:12

PythonEnthusiast


1 Answers

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

like image 165
sk1p Avatar answered Nov 15 '22 19:11

sk1p