[reportlab-users] Two bugs with Table with Paragraph as cell content

Yuan HOng hongyuan1306 at gmail.com
Sat Jun 9 12:25:01 EDT 2007


I have found two bugs working with paragraph objects as Table cell contents.

First, If I span a region in the table, and the cell to the right of
the starting cell in the span contains a paragraph, the processing
fails with an error message:

Traceback (most recent call last):
File "testPDF.py", line 27, in <module>
doc.build(story)
File "c:\python25\lib\site-packages\reportlab\platypus\doctemplate.py",
line 911, in build
BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
File "c:\python25\lib\site-packages\reportlab\platypus\doctemplate.py",
line 740, in build
self.handle_flowable(flowables)
File "c:\python25\lib\site-packages\reportlab\platypus\doctemplate.py",
line 638, in handle_flowable
if frame.add(f, self.canv, trySplit=self.allowSplitting):
File "c:\python25\lib\site-packages\reportlab\platypus\frames.py",
line 141, in _add
w, h = flowable.wrap(aW, h)
File "c:\python25\lib\site-packages\reportlab\platypus\tables.py",
line 1031, in wrap
self._calc(availWidth, availHeight)
File "c:\python25\lib\site-packages\reportlab\platypus\tables.py",
line 527, in _calc
self._calc_height(availHeight,availWidth,W=W)
File "c:\python25\lib\site-packages\reportlab\platypus\tables.py",
line 480, in _calc_height
w = max(colpositions[t[2]+1]-colpositions[t[0]],w)
TypeError: 'NoneType' object is unsubscriptable

The test file that produces this error is attached as follows:

# -*- coding: utf-8 -*-

from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors

stylesheet=getSampleStyleSheet()
normalStyle=stylesheet['Normal']

story = []

p = Paragraph('This is cell one that contains a paragraph.', normalStyle)

data = [['Col1', 'Col2', 'Col3', 'Col4', 'Col5']]
data.extend([['01', p, '02', '03', '04']]*50)

t = Table(data, ['20%']*5, repeatRows=1)
t.setStyle(TableStyle([
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
('SPAN', (0,50), (-2,50)),
]))

story.append(t)

doc = SimpleDocTemplate('table.pdf')
doc.build(story)

This bug can be fixed with the following patch against the current SVN head:

Index: platypus/tables.py
===================================================================
--- platypus/tables.py (revision 3104)
+++ platypus/tables.py (working copy)
@@ -475,7 +475,7 @@
if w is None and not self._canGetWidth(v):
raise ValueError, "Flowable %s in
cell(%d,%d) can't have auto width in\n%s" %
(v[0].identity(30),i,j,self.identity(30))
if canv: canv._fontname, canv._fontsize,
canv._leading = s.fontname, s.fontsize, s.leading or 1.2*s.fontsize
- if ji in colSpanCells:
+ if ji in colSpanCells and spanRanges[ji]:
t = spanRanges[ji]
w =
max(colpositions[t[2]+1]-colpositions[t[0]],w)
dW,t = self._listCellGeom(v,w or
self._listValueWidth(v),s)

The second bug is that when specifying spans using -1 to indicate the
last line, the span is repeated on every page where the table breaks.
And sometimes the table header is repeated on the same page. The
following test file will illustrate this bug:

# -*- coding: utf-8 -*-

from reportlab.platypus import Paragraph, SimpleDocTemplate, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib import colors

stylesheet=getSampleStyleSheet()
normalStyle=stylesheet['Normal']

story = []

p = Paragraph('This is cell one that contains a paragraph.', normalStyle)

data = [['Col1', 'Col2', 'Col3', 'Col4', 'Col5']]
data.extend([[p, '01', '02', '03', '04']]*50)

t = Table(data, ['20%']*5, repeatRows=1)
t.setStyle(TableStyle([
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('BOX', (0,0), (-1,-1), 0.25, colors.black),
('SPAN', (0,-1), (-2,-1)),
]))

story.append(t)

doc = SimpleDocTemplate('table.pdf')
doc.build(story)

This would produce a very strange output. Hope somebody can fix this one.

--
Hong Yuan

大管家网上建材超市
装修装潢建材一站式购物
http://www.homemaster.cn


More information about the reportlab-users mailing list