【问题标题】:How do I continue a content to a next page in Reportlabs - Python如何在 Reportlabs 中将内容继续到下一页 - Python
【发布时间】:2013-12-23 08:13:26
【问题描述】:

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

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

所以,我的问题是如何在不使用 showpage() 的情况下将内容继续到 Reportlabs 中的下一页,因为我无法知道何时点击 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))

【问题讨论】:

  • 我也有同样的问题..知道答案很有趣..

标签: python pdf pdf-generation reportlab


【解决方案1】:

我建议使用更高级的 reportlab 原语,即文档模板、框架和可流动对象。这样,您就可以“免费”拆分。 An example from the related questions

【讨论】:

    【解决方案2】:

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

    如果有帮助请告诉我:)

    【讨论】:

    • 不,它不起作用。我仍然得到仅限于 1 页的数据。其他数据被截断..
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多