在 PDF 页面上拆分 ReportLab 表(并排)?

发布于 2025-01-07 02:55:37 字数 1766 浏览 2 评论 0原文

下面的代码创建了一个很好的测试表,其中包含 99 行数据和在每个分页符处重复的标题。该表非常窄,所以我试图弄清楚如何将其拆分,以便它在第一页的左侧具有第 1-37 行,在第一页的右侧具有第 38-74 行,并且第二页左侧第 75-99 行。我称之为“在页面上拆分表格”,但对于我正在尝试做的事情可能有一个更好的名称,所以我希望我已经准确地描述了它。

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Frame, Spacer
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A3, A4, landscape, portrait
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.pdfgen import canvas

pdfReportPages = "C:\\Temp\\test.pdf"
doc = SimpleDocTemplate(pdfReportPages, pagesize=A4)

# container for the "Flowable" objects
elements = []
styles=getSampleStyleSheet()
styleN = styles["Normal"]

# Make heading for each column and start data list
column1Heading = "COL ONE"
column2Heading = "COL TWO"
# Assemble data for each column using simple loop to append it into data list
data = [[column1Heading,column2Heading]]
for i in range(1,100):
    data.append(["Col 1 Row " + str(i),"Col 2 Row " + str(i)])

tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * cm], repeatRows=1)
tableThatSplitsOverPages.hAlign = 'LEFT'
tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black),
                       ('VALIGN',(0,0),(-1,-1),'TOP'),
                       ('LINEBELOW',(0,0),(-1,-1),1,colors.black),
                       ('BOX',(0,0),(-1,-1),1,colors.black),
                       ('BOX',(0,0),(0,-1),1,colors.black)])
tblStyle.add('BACKGROUND',(0,0),(1,0),colors.lightblue)
tblStyle.add('BACKGROUND',(0,1),(-1,-1),colors.white)
tableThatSplitsOverPages.setStyle(tblStyle)
elements.append(tableThatSplitsOverPages)

doc.build(elements)

The code below creates a nice test table with 99 rows of data and a header that gets repeated at each page break. The table is quite narrow so I am trying to figure out how to make it split so that it has rows 1-37 on the left hand side of the first page, rows 38-74 on the right hand side of the first page, and rows 75-99 on the left hand side of the second page. I've called this "splitting a table across a page" but there may be a better name for what I am trying to do so I hope I have described it accurately.

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Frame, Spacer
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A3, A4, landscape, portrait
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.pdfgen import canvas

pdfReportPages = "C:\\Temp\\test.pdf"
doc = SimpleDocTemplate(pdfReportPages, pagesize=A4)

# container for the "Flowable" objects
elements = []
styles=getSampleStyleSheet()
styleN = styles["Normal"]

# Make heading for each column and start data list
column1Heading = "COL ONE"
column2Heading = "COL TWO"
# Assemble data for each column using simple loop to append it into data list
data = [[column1Heading,column2Heading]]
for i in range(1,100):
    data.append(["Col 1 Row " + str(i),"Col 2 Row " + str(i)])

tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * cm], repeatRows=1)
tableThatSplitsOverPages.hAlign = 'LEFT'
tblStyle = TableStyle([('TEXTCOLOR',(0,0),(-1,-1),colors.black),
                       ('VALIGN',(0,0),(-1,-1),'TOP'),
                       ('LINEBELOW',(0,0),(-1,-1),1,colors.black),
                       ('BOX',(0,0),(-1,-1),1,colors.black),
                       ('BOX',(0,0),(0,-1),1,colors.black)])
tblStyle.add('BACKGROUND',(0,0),(1,0),colors.lightblue)
tblStyle.add('BACKGROUND',(0,1),(-1,-1),colors.white)
tableThatSplitsOverPages.setStyle(tblStyle)
elements.append(tableThatSplitsOverPages)

doc.build(elements)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2025-01-14 02:55:38

您将需要使用 PageTemplates 来完成此任务,方法是创建一个具有多个框架的 PageTemplate,这些框架将允许您指定内容区域以在页面内绘制文档。不幸的是,这意味着放弃 SimpleDocTemplate 并使用 BaseDocTemplate 并提供您自己的 PageTemplates(以及其他需要的东西)。

You will need to use PageTemplates to accomplish this by creating a PageTemplate that has multiple Frames that will allow you to specify content areas to draw the document within the page. This unfortunately means abandoning SimpleDocTemplate and instead using BaseDocTemplate and supplying your own PageTemplates (as well as other things if you want them).

当梦初醒 2025-01-14 02:55:37

如果您知道页面上的确切行数,则可以使用此函数来模拟两列。这样,表格仍然自动流过多个页面,您不必担心页面模板。

def columnize(data, interval):
    ret = []
    for i in range(0, len(data), interval * 2):
        for j in range(min(interval, len(data) - i)):
            ret.append(data[i + j] + (data[i + j + interval] if i + j + interval < len(data) else []))
    return ret

在您的示例中使用:

data = columnize(data, 75)
tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * cm], repeatRows=1)

If you know the exact number of rows on the page you can use this function to simulate two columns. That way the the table still automatically flows over multiple pages and you don't have to worry about PageTemplates.

def columnize(data, interval):
    ret = []
    for i in range(0, len(data), interval * 2):
        for j in range(min(interval, len(data) - i)):
            ret.append(data[i + j] + (data[i + j + interval] if i + j + interval < len(data) else []))
    return ret

Use in your example:

data = columnize(data, 75)
tableThatSplitsOverPages = Table(data, [2.5 * cm, 2.5 * cm], repeatRows=1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文