1

我正在生成一个足够长的表格,可以进入第二页。我想要实现的是在第二页上跨越 4 行。我使用 TableStyle 中的以下代码来执行此操作。

('SPAN', (0,38), (0,41))

两行的跨度虽然有效。('跨度',(0,38),(0,39))

这是一个代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from reportlab.platypus import SimpleDocTemplate, Table, LongTable, TableStyle, BaseDocTemplate, Frame, Paragraph, NextPageTemplate, PageTemplate
from reportlab.lib.pagesizes import letter, inch
from reportlab.lib import colors

def testPdf():
    doc = BaseDocTemplate("testpdf.pdf",pagesize=letter,
                        rightMargin=72,leftMargin=72,
                        topMargin=72,bottomMargin=18, showBoundary=True)
    width, height = letter
    print width 
    print height

    elements = []
    datas = []
    for x in range(1,50):
        datas.append(
            [x,x+1]
        )
    t=LongTable(datas)

    tTableStyle=[
        ('SPAN', (0,38), (0,41)),
        ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
        ('BOX', (0,0), (-1,-1), 0.25, colors.black),
    ]
    t.setStyle(TableStyle(tTableStyle))
    elements.append(t)

    frameT = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal')

    doc.addPageTemplates([PageTemplate(id='OneCol',frames=frameT)])

    doc.build(elements)

if __name__ == '__main__':
    testPdf()
4

1 回答 1

1

我运行了您的示例代码并看到了同样的问题。对于任何可能感兴趣的人,导致的错误是:

Traceback (most recent call last):
  File "./test.py", line 39, in <module>
    testPdf()
  File "./test.py", line 36, in testPdf
    doc.build(elements)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 880, in build
    self.handle_flowable(flowables)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 763, in handle_flowable
    if frame.add(f, canv, trySplit=self.allowSplitting):
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/frames.py", line 159, in _add
    w, h = flowable.wrap(aW, h)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 1113, in wrap
    self._calc(availWidth, availHeight)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 587, in _calc
    self._calc_height(availHeight,availWidth,W=W)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 553, in _calc_height
    spanFixDim(H0,H,spanCons,lim=hmax)
  File "/usr/local/greenplum-db/ext/python/lib/python2.6/site-packages/reportlab/platypus/tables.py", line 205, in spanFixDim
    t = sum([V[x]+M.get(x,0) for x in xrange(x0,x1)])
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'

排序答案:这是 ReportLab 中的一个错误,您需要将其报告给开发人员。

长答案:我以前见过分裂的问题。当拆分显然是可能的时,存在(并且显然仍然存在)混淆算法的情况,但算法最终抛出错误,因为它无法确定将其放在哪个页面上。它可能发生在您意想不到的奇怪时间,而且我知道除了自己手动拆分事物(或将内容包装在KeepTogether可流动的内容中,但这在您的情况下不起作用)之外没有特别好的解决方法。

于 2012-04-24T01:01:29.987 回答