Danfi
Danfi

Reputation: 1272

Reportlab: use Table and SPAN

Example:

#!/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()

this code runs success, because the table is in one page,if I set the "SPAN" to "(0,0),(0,38)" ,the error is:

reportlab.platypus.doctemplate.LayoutError: Flowable with cell(0,0) containing
'1'(46.24 x 702) too large on page 2 in frame 'normal'(456.0 x 690.0*) of template 'Later'

and if I set it bigger the error will be:

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'

How can I deal with this?

Upvotes: 1

Views: 5100

Answers (1)

Nicholas TJ
Nicholas TJ

Reputation: 1649

The reason you're facing this problem is exactly what Gordon Worley commented above. There's no way to SPAN across page automatically as the algorithm implemented will be confused of the height and width calculated.

An approach to tackle this will be manually format/style your table per page using row/column coordinates. Sadly, even the replies in the reportlab suggest we do this manually.

I did split my tables manually and style them separately, which in my opinion is a very ugly approach. I'll look for other alternatives later.

For reference: https://bitbucket.org/ntj/reportlab_imko_table

Upvotes: 2

Related Questions