如何在 Reportlabs 中将内容继续到下一页 - Python

Posted

技术标签:

【中文标题】如何在 Reportlabs 中将内容继续到下一页 - Python【英文标题】:How do I continue a content to a next page in 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))

【问题讨论】:

我也有同样的问题..知道答案很有趣.. 【参考方案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 页的数据。其他数据被截断..

以上是关于如何在 Reportlabs 中将内容继续到下一页 - Python的主要内容,如果未能解决你的问题,请参考以下文章

确保在转到下一页之前发送了Google Analytics事件

如果不满足条件,则停止前进到下一页

css点击a链接到下一页底部

在WPS文字中的表格中打字,表格就自动跳到下一页?

如何知道用户何时在 PDFView 中滑动到下一页? - 安卓

Xpages-如何在没有人工交互的情况下自动切换到下一页?