【问题标题】:Reportlab: use Table and SPANReportlab:使用表和 SPAN
【发布时间】:2011-11-01 08:37:44
【问题描述】:

例子:

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

from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib.pagesizes import letter

def testPdf():
    doc = SimpleDocTemplate("testpdf.pdf",pagesize=letter,
                        rightMargin=72,leftMargin=72,
                        topMargin=72,bottomMargin=18)
    elements = []
    datas = []
    for x in range(1,50):
        datas.append(
            [x,x+1]
        )
    t=Table(datas)
    tTableStyle=[
        ('SPAN',(0,0),(0,37)),
      ]
    t.setStyle(TableStyle(tTableStyle))
    elements.append(t)
    doc.build(elements)

if __name__ == '__main__':
    testPdf()

此代码运行成功,因为表格在一页中,如果我将“SPAN”设置为“(0,0),(0,38)”,错误是:

reportlab.platypus.doctemplate.LayoutError: Flowable with cell(0,0) contains
模板“稍后”的“正常”(456.0 x 690.0*) 帧中的第 2 页上的“1”(46.24 x 702) 太大

如果我将它设置得更大,错误将是:

Traceback (most recent call last):
  File "testpdf.py", line 26, in <module>
    testPdf()
  File "testpdf.py", line 23, in testPdf
    doc.build(elements)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 1117, in build
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 880, in build
    self.handle_flowable(flowables)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py", line 763, in handle_flowable
    if frame.add(f, canv, trySplit=self.allowSplitting):
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/frames.py", line 159, in _add
    w, h = flowable.wrap(aW, h)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 1113, in wrap
    self._calc(availWidth, availHeight)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 587, in _calc
    self._calc_height(availHeight,availWidth,W=W)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py", line 553, in _calc_height
    spanFixDim(H0,H,spanCons,lim=hmax)
  File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/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'

我该如何处理?

【问题讨论】:

  • 你想看到什么行为?您收到此错误的原因正是您所期望的:表格单元格太大而无法在单个框架中显示。您需要以某种方式使框架更大或拆分单元格。
  • 有什么方法可以自动跨页制作“SPAN”?
  • 我不知道。我很确定这从根本上违背了渲染单元格的工作方式。
  • 谢谢!看来他们不会改进表格。

标签: python html reportlab


【解决方案1】:

您遇到此问题的原因正是 Gordon Worley 上面所评论的。没有办法自动跨页跨页,因为实现的算法会混淆计算的高度和宽度。

解决此问题的一种方法是使用行/列坐标手动设置每页表格的格式/样式。可悲的是,即使是报告实验室中的回复也建议我们手动执行此操作。

我确实手动拆分了我的表格并分别设置它们的样式,在我看来这是一种非常丑陋的方法。稍后我会寻找其他替代方案。

供参考:https://bitbucket.org/ntj/reportlab_imko_table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 1970-01-01
    • 2022-08-04
    • 1970-01-01
    相关资源
    最近更新 更多