Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Reportlab:使用表和跨度_Python_Html_Reportlab - Fatal编程技术网

Python Reportlab:使用表和跨度

Python Reportlab:使用表和跨度,python,html,reportlab,Python,Html,Reportlab,例如: #!/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,

例如:

#!/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.DoctTemplate.LayoutError:可流动,单元格(0,0)包含
模板“以后”的框架“正常”(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'
回溯(最近一次呼叫最后一次):
文件“testpdf.py”,第26行,在
testPdf()
testpdf中的文件“testpdf.py”,第23行
文档生成(元素)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py”,第1117行,内部版本
BaseDocTemplate.build(self、flowables、canvasmaker=canvasmaker)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py”,第880行,内部版本
自身手柄可流动(可流动)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/doctemplate.py”,第763行,在handle_flowable中
如果frame.add(f,canv,trySplit=self.allowspliting):
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/frames.py”,第159行,添加
w、 h=可流动包裹(aW,h)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py”,第1113行,换行
自身计算(有效宽度、有效高度)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py”,第587行,在计算中
自计算高度(有效高度,有效宽度,W=W)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py”,第553行,在计算高度中
spanFixDim(H0,H,spanCons,lim=hmax)
文件“/usr/local/lib/python2.7/dist packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/platypus/tables.py”,第205行,在spanFixDim中
t=总和([V[x]+M.get(x,0)表示x范围内的x(x0,x1)])
TypeError:不支持+:“NoneType”和“int”的操作数类型

我该怎么处理

你面临这个问题的原因正是戈登·沃利(Gordon Worley)在上面的评论。无法自动跨越页面,因为实现的算法将混淆计算的高度和宽度

解决这一问题的方法是使用行/列坐标手动设置每页表格的格式/样式。遗憾的是,即使reportlab中的回复也建议我们手动执行此操作

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


仅供参考:

您希望看到什么行为?出现此错误的原因正是您所期望的:表格单元格太大,无法在单个帧中显示。你需要以某种方式使框架变大或分割单元格。有没有办法自动在整个页面上形成“跨度”呢?我不知道。我很确定这从根本上违背了渲染单元的工作原理。谢谢!看来他们不会改进桌子。