Search code examples
pythonreportlab

Python - Reportlab: Why do SPAN for more than two rows at the second page returns error?


I'm generating a table that is long enough to go onto second page. What I would like to achieve is to SPAN 4 rows on the second page. I do it using the follwing code in the TableStyle.

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

Span for two rows works though. ('SPAN', (0,38), (0,39))

Here's a code:

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

Solution

  • I ran your sample code and saw the same problem. For anyone coming along who might be interested, the error that results is:

    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'
    

    Sort answer: this is a bug in ReportLab and you'll need to report it to the developers.

    Long answer: I've seen problems with splitting before. There were (and apparently still are) situations that confuse the algorithm when splitting is plainly possible but the algorithm ends up throwing an error because it can't figure out which page to put it on. It can happen at weird times when you wouldn't expect it, and I know of no especially good work around other than to manually split things yourself (or to wrap content in KeepTogether flowables, but that won't work in your situation).