author | rgbecker |
Wed, 25 Oct 2000 08:57:46 +0000 | |
changeset 494 | 54257447cfe9 |
parent 421 | 6dd1e6e707b7 |
child 496 | bb47cf5c2739 |
permissions | -rwxr-xr-x |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/tables.py?cvsroot=reportlab |
|
4 |
#$Header: /tmp/reportlab/reportlab/platypus/tables.py,v 1.31 2000/10/25 08:57:45 rgbecker Exp $ |
|
5 |
__version__=''' $Id: tables.py,v 1.31 2000/10/25 08:57:45 rgbecker Exp $ ''' |
|
16 | 6 |
__doc__=""" |
6 | 7 |
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
8 |
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the |
|
9 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
268 | 10 |
|
327 | 11 |
None values in the sequence of row heights or column widths, mean that the corresponding rows |
12 |
or columns should be automatically sized. |
|
13 |
||
14 |
All the cell values should be convertible to strings; embedded newline '\\n' characters |
|
15 |
cause the value to wrap (ie are like a traditional linefeed). |
|
16 |
||
268 | 17 |
See the test output from running this module as a script for a discussion of the method for constructing |
18 |
tables and table styles. |
|
6 | 19 |
""" |
253 | 20 |
from reportlab.platypus import * |
129 | 21 |
from reportlab.lib.styles import PropertySet, getSampleStyleSheet |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
22 |
from reportlab.lib import colors |
253 | 23 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE |
403 | 24 |
from reportlab.pdfbase import pdfmetrics |
312 | 25 |
import operator, string |
6 | 26 |
|
421 | 27 |
from types import TupleType, ListType, StringType |
6 | 28 |
|
128 | 29 |
class CellStyle(PropertySet): |
326 | 30 |
defaults = { |
31 |
'fontname':'Times-Roman', |
|
32 |
'fontsize':10, |
|
33 |
'leading':12, |
|
34 |
'leftPadding':6, |
|
35 |
'rightPadding':6, |
|
36 |
'topPadding':3, |
|
37 |
'bottomPadding':3, |
|
38 |
'firstLineIndent':0, |
|
39 |
'color':colors.black, |
|
40 |
'alignment': 'LEFT', |
|
41 |
'background': (1,1,1), |
|
329 | 42 |
'valign': 'BOTTOM', |
326 | 43 |
} |
6 | 44 |
|
45 |
class TableStyle: |
|
326 | 46 |
def __init__(self, cmds=None): |
47 |
self._cmds = cmds |
|
48 |
if cmds is None: |
|
49 |
self._cmds = [] |
|
50 |
def add(self, *cmd): |
|
51 |
self._cmds.append(cmd) |
|
52 |
def getCommands(self): |
|
53 |
return self._cmds |
|
338 | 54 |
|
55 |
TableStyleType = type(TableStyle()) |
|
419 | 56 |
_SeqTypes = (TupleType, ListType) |
356 | 57 |
|
58 |
def _rowLen(x): |
|
419 | 59 |
return type(x) not in _SeqTypes and 1 or len(x) |
60 |
||
421 | 61 |
def _listCellGeom(V,w,s,W=None,H=None): |
419 | 62 |
aW = w-s.leftPadding-s.rightPadding |
63 |
t = 0 |
|
64 |
w = 0 |
|
65 |
for v in V: |
|
66 |
vw, vh = v.wrap(aW, 72000) |
|
67 |
if W is not None: W.append(vw) |
|
421 | 68 |
if H is not None: H.append(vh) |
419 | 69 |
w = max(w,vw) |
70 |
t = t + vh + v.getSpaceBefore()+v.getSpaceAfter() |
|
71 |
return w, t - V[0].getSpaceBefore()-V[-1].getSpaceAfter() |
|
356 | 72 |
|
221 | 73 |
class Table(Flowable): |
356 | 74 |
def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
350 | 75 |
repeatRows=0, repeatCols=0, splitByRow=1): |
356 | 76 |
nrows = len(data) |
419 | 77 |
if len(data)==0 or type(data) not in _SeqTypes: |
356 | 78 |
raise ValueError, "Table must have at least 1 row" |
79 |
ncols = max(map(_rowLen,data)) |
|
80 |
if not ncols: |
|
326 | 81 |
raise ValueError, "Table must have at least 1 column" |
356 | 82 |
if colWidths is None: colWidths = ncols*[None] |
83 |
elif len(colWidths) != ncols: |
|
84 |
raise ValueError, "Data error - %d columns in data but %d in grid" % (ncols, len(colWidths)) |
|
85 |
if rowHeights is None: rowHeights = nrows*[None] |
|
86 |
elif len(rowHeights) != nrows: |
|
87 |
raise ValueError, "Data error - %d rows in data but %d in grid" % (nrows, len(rowHeights)) |
|
88 |
self._nrows = nrows |
|
326 | 89 |
ncols = self._ncols = len(colWidths) |
90 |
for i in range(nrows): |
|
91 |
if len(data[i]) != ncols: |
|
92 |
raise ValueError, "Not enough data points in row %d!" % i |
|
350 | 93 |
self._rowHeights = self._argH = rowHeights |
94 |
self._colWidths = self._argW = colWidths |
|
326 | 95 |
self._cellvalues = data |
96 |
dflt = CellStyle('<default>') |
|
350 | 97 |
|
98 |
self._cellStyles = [None]*nrows |
|
326 | 99 |
for i in range(nrows): |
350 | 100 |
self._cellStyles[i] = [dflt]*ncols |
101 |
||
326 | 102 |
self._bkgrndcmds = [] |
103 |
self._linecmds = [] |
|
104 |
self._curweight = self._curcolor = self._curcellstyle = None |
|
350 | 105 |
self.repeatRows = repeatRows |
106 |
self.repeatCols = repeatCols |
|
107 |
self.splitByRow = splitByRow |
|
6 | 108 |
|
342 | 109 |
if style: |
110 |
self.setStyle(style) |
|
111 |
||
326 | 112 |
def _calc(self): |
403 | 113 |
if hasattr(self,'_width'): return |
333 | 114 |
|
350 | 115 |
H = self._argH |
116 |
W = self._argW |
|
6 | 117 |
|
326 | 118 |
if None in H: |
119 |
H = H[:] #make a copy as we'll change it |
|
333 | 120 |
self._rowHeights = H |
326 | 121 |
while None in H: |
122 |
i = H.index(None) |
|
123 |
V = self._cellvalues[i] |
|
350 | 124 |
S = self._cellStyles[i] |
326 | 125 |
h = 0 |
419 | 126 |
for v, s, w in map(None, V, S, W): |
127 |
t = type(v) |
|
421 | 128 |
if t in _SeqTypes or isinstance(v,Flowable): |
129 |
if not t in _SeqTypes: v = (v,) |
|
419 | 130 |
if w is None: |
131 |
raise ValueError, "Flowables cell can't have auto width" |
|
132 |
dummy,t = _listCellGeom(v,w,s) |
|
133 |
else: |
|
421 | 134 |
if t is not StringType: |
135 |
v = v is None and '' or str(v) |
|
419 | 136 |
v = string.split(v, "\n") |
421 | 137 |
t = s.leading*len(v) |
138 |
t = t+s.bottomPadding+s.topPadding |
|
326 | 139 |
if t>h: h = t #record a new maximum |
140 |
H[i] = h |
|
6 | 141 |
|
326 | 142 |
if None in W: |
143 |
W = W[:] |
|
144 |
self._colWidths = W |
|
145 |
while None in W: |
|
146 |
i = W.index(None) |
|
147 |
f = lambda x,i=i: operator.getitem(x,i) |
|
148 |
V = map(f,self._cellvalues) |
|
350 | 149 |
S = map(f,self._cellStyles) |
326 | 150 |
w = 0 |
403 | 151 |
d = hasattr(self,'canv') and self.canv or pdfmetrics |
326 | 152 |
for v, s in map(None, V, S): |
419 | 153 |
t = type(v) |
421 | 154 |
if t in _SeqTypes or isinstance(v,Flowable): |
419 | 155 |
raise ValueError, "Flowables cell can't have auto width" |
421 | 156 |
elif t is not StringType: v = v is None and '' or str(v) |
326 | 157 |
v = string.split(v, "\n") |
158 |
t = s.leftPadding+s.rightPadding + max(map(lambda a, b=s.fontname, |
|
403 | 159 |
c=s.fontsize,d=d.stringWidth: d(a,b,c), v)) |
326 | 160 |
if t>w: w = t #record a new maximum |
161 |
W[i] = w |
|
6 | 162 |
|
326 | 163 |
height = self._height = reduce(operator.add, H, 0) |
164 |
self._rowpositions = [height] # index 0 is actually topline; we skip when processing cells |
|
165 |
for h in H: |
|
166 |
height = height - h |
|
167 |
self._rowpositions.append(height) |
|
329 | 168 |
assert abs(height)<1e-8, 'Internal height error' |
326 | 169 |
width = 0 |
170 |
self._colpositions = [0] #index -1 is right side boundary; we skip when processing cells |
|
171 |
for w in W: |
|
172 |
width = width + w |
|
173 |
self._colpositions.append(width) |
|
174 |
self._width = width |
|
6 | 175 |
|
326 | 176 |
def setStyle(self, tblstyle): |
338 | 177 |
if type(tblstyle) is not TableStyleType: |
178 |
tblstyle = TableStyle(tblstyle) |
|
326 | 179 |
for cmd in tblstyle.getCommands(): |
350 | 180 |
self._addCommand(cmd) |
181 |
||
182 |
def _addCommand(self,cmd): |
|
183 |
if cmd[0] == 'BACKGROUND': |
|
184 |
self._bkgrndcmds.append(cmd) |
|
185 |
elif _isLineCommand(cmd): |
|
186 |
self._linecmds.append(cmd) |
|
187 |
else: |
|
188 |
(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:] |
|
189 |
if sc < 0: sc = sc + self._ncols |
|
190 |
if ec < 0: ec = ec + self._ncols |
|
191 |
if sr < 0: sr = sr + self._nrows |
|
192 |
if er < 0: er = er + self._nrows |
|
193 |
for i in range(sr, er+1): |
|
194 |
for j in range(sc, ec+1): |
|
195 |
_setCellStyle(self._cellStyles, i, j, op, values) |
|
326 | 196 |
|
197 |
def _drawLines(self): |
|
198 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
|
199 |
if sc < 0: sc = sc + self._ncols |
|
200 |
if ec < 0: ec = ec + self._ncols |
|
201 |
if sr < 0: sr = sr + self._nrows |
|
202 |
if er < 0: er = er + self._nrows |
|
403 | 203 |
getattr(self,_LineOpMap.get(op, '_drawUnknown' ))( (sc, sr), (ec, er), weight, color) |
326 | 204 |
self._curcolor = None |
248 | 205 |
|
403 | 206 |
def _drawUnknown(self, (sc, sr), (ec, er), weight, color): |
207 |
raise ValueError, "Unknown line command '%s'" % op |
|
208 |
||
209 |
def _drawGrid(self, (sc, sr), (ec, er), weight, color): |
|
210 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
211 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
212 |
||
326 | 213 |
def _drawBox(self, (sc, sr), (ec, er), weight, color): |
214 |
self._drawHLines((sc, sr), (ec, sr), weight, color) |
|
215 |
self._drawHLines((sc, er+1), (ec, er+1), weight, color) |
|
216 |
self._drawVLines((sc, sr), (sc, er), weight, color) |
|
217 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color) |
|
350 | 218 |
|
326 | 219 |
def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color): |
220 |
self._drawHLines((sc, sr+1), (ec, er), weight, color) |
|
221 |
self._drawVLines((sc+1, sr), (ec, er), weight, color) |
|
350 | 222 |
|
326 | 223 |
def _prepLine(self, weight, color): |
224 |
if color != self._curcolor: |
|
225 |
self.canv.setStrokeColor(color) |
|
226 |
self._curcolor = color |
|
227 |
if weight != self._curweight: |
|
228 |
self.canv.setLineWidth(weight) |
|
229 |
self._curweight = weight |
|
350 | 230 |
|
326 | 231 |
def _drawHLines(self, (sc, sr), (ec, er), weight, color): |
232 |
self._prepLine(weight, color) |
|
233 |
scp = self._colpositions[sc] |
|
234 |
ecp = self._colpositions[ec+1] |
|
235 |
for rowpos in self._rowpositions[sr:er+1]: |
|
236 |
self.canv.line(scp, rowpos, ecp, rowpos) |
|
350 | 237 |
|
403 | 238 |
def _drawHLinesB(self, (sc, sr), (ec, er), weight, color): |
239 |
self._drawHLines((sc, sr+1), (ec, er+1), weight, color) |
|
240 |
||
326 | 241 |
def _drawVLines(self, (sc, sr), (ec, er), weight, color): |
242 |
self._prepLine(weight, color) |
|
243 |
srp = self._rowpositions[sr] |
|
244 |
erp = self._rowpositions[er+1] |
|
245 |
for colpos in self._colpositions[sc:ec+1]: |
|
246 |
self.canv.line(colpos, srp, colpos, erp) |
|
247 |
||
403 | 248 |
def _drawVLinesA(self, (sc, sr), (ec, er), weight, color): |
249 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color) |
|
250 |
||
326 | 251 |
def wrap(self, availWidth, availHeight): |
252 |
self._calc() |
|
253 |
#nice and easy, since they are predetermined size |
|
254 |
self.availWidth = availWidth |
|
255 |
return (self._width, self._height) |
|
350 | 256 |
|
419 | 257 |
def onSplit(self,T,byRow=1): |
258 |
''' |
|
259 |
This method will be called when the Table is split. |
|
260 |
Special purpose tables can override to do special stuff. |
|
261 |
''' |
|
262 |
pass |
|
263 |
||
350 | 264 |
def _cr_0(self,n,cmds): |
354 | 265 |
for c in cmds: |
266 |
c = tuple(c) |
|
267 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 268 |
if sr>=n: continue |
269 |
if er>=n: er = n-1 |
|
354 | 270 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 271 |
|
272 |
def _cr_1_1(self,n,repeatRows, cmds): |
|
354 | 273 |
for c in cmds: |
274 |
c = tuple(c) |
|
275 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 276 |
if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue |
277 |
if sr>=repeatRows and sr<n: sr=repeatRows |
|
278 |
elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n |
|
279 |
if er>=repeatRows and er<n: er=repeatRows |
|
280 |
elif er>=repeatRows and er>=n: er=er+repeatRows-n |
|
354 | 281 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 282 |
|
283 |
def _cr_1_0(self,n,cmds): |
|
354 | 284 |
for c in cmds: |
285 |
c = tuple(c) |
|
286 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 287 |
if er>=0 and er<n: continue |
288 |
if sr>=0 and sr<n: sr=0 |
|
289 |
if sr>=n: sr = sr-n |
|
290 |
if er>=n: er = er-n |
|
354 | 291 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 292 |
|
293 |
def _splitRows(self,availHeight): |
|
294 |
h = 0 |
|
295 |
n = 0 |
|
296 |
lim = len(self._rowHeights) |
|
297 |
while n<lim: |
|
298 |
hn = h + self._rowHeights[n] |
|
299 |
if hn>availHeight: break |
|
300 |
h = hn |
|
301 |
n = n + 1 |
|
302 |
||
303 |
if n<=self.repeatRows: |
|
304 |
return [] |
|
305 |
||
306 |
if n==lim: return [self] |
|
307 |
||
308 |
repeatRows = self.repeatRows |
|
309 |
repeatCols = self.repeatCols |
|
310 |
splitByRow = self.splitByRow |
|
311 |
data = self._cellvalues |
|
312 |
||
313 |
#we're going to split into two superRows |
|
356 | 314 |
R0 = Table( data[:n], self._argW, self._argH[:n], |
350 | 315 |
repeatRows=repeatRows, repeatCols=repeatCols, |
316 |
splitByRow=splitByRow) |
|
317 |
||
318 |
#copy the styles and commands |
|
319 |
R0._cellStyles = self._cellStyles[:n] |
|
419 | 320 |
|
321 |
A = [] |
|
322 |
# hack up the line commands |
|
323 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
|
324 |
if sc < 0: sc = sc + self._ncols |
|
325 |
if ec < 0: ec = ec + self._ncols |
|
326 |
if sr < 0: sr = sr + self._nrows |
|
327 |
if er < 0: er = er + self._nrows |
|
328 |
||
329 |
if op in ('BOX','OUTLINE','GRID'): |
|
330 |
if sr<n and er>=n: |
|
331 |
# we have to split the BOX |
|
332 |
A.append(('LINEABOVE',(sc,sr), (ec,sr), weight, color)) |
|
333 |
A.append(('LINEBEFORE',(sc,sr), (sc,er), weight, color)) |
|
334 |
A.append(('LINEAFTER',(ec,sr), (ec,er), weight, color)) |
|
335 |
A.append(('LINEBELOW',(sc,er), (ec,er), weight, color)) |
|
336 |
if op=='GRID': |
|
337 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
421 | 338 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
419 | 339 |
A.append(('INNERGRID',(sc,sr), (ec,er), weight, color)) |
340 |
else: |
|
341 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
342 |
elif op in ('INNERGRID','LINEABOVE'): |
|
343 |
if sr<n and er>=n: |
|
344 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
421 | 345 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
419 | 346 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
347 |
elif op == 'LINEBELOW': |
|
421 | 348 |
if sr<n and er>=(n-1): |
419 | 349 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
350 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
421 | 351 |
elif op == 'LINEABOVE': |
352 |
if sr<=n and er>=n: |
|
353 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
354 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
419 | 355 |
else: |
356 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
357 |
||
358 |
R0._cr_0(n,A) |
|
350 | 359 |
R0._cr_0(n,self._bkgrndcmds) |
360 |
||
361 |
if repeatRows: |
|
356 | 362 |
R1 = Table(data[:repeatRows]+data[n:], |
363 |
self._argW, self._argH[:repeatRows]+self._argH[n:], |
|
350 | 364 |
repeatRows=repeatRows, repeatCols=repeatCols, |
365 |
splitByRow=splitByRow) |
|
366 |
R1._cellStyles = self._cellStyles[:repeatRows]+self._cellStyles[n:] |
|
419 | 367 |
R1._cr_1_1(n,repeatRows,A) |
350 | 368 |
R1._cr_1_1(n,repeatRows,self._bkgrndcmds) |
369 |
else: |
|
356 | 370 |
R1 = Table(data[n:], self._argW, self._argH[n:], |
350 | 371 |
repeatRows=repeatRows, repeatCols=repeatCols, |
372 |
splitByRow=splitByRow) |
|
373 |
R1._cellStyles = self._cellStyles[n:] |
|
419 | 374 |
R1._cr_1_0(n,A) |
350 | 375 |
R1._cr_1_0(n,self._bkgrndcmds) |
376 |
||
419 | 377 |
self.onSplit(R0) |
378 |
self.onSplit(R1) |
|
350 | 379 |
return [R0,R1] |
380 |
||
381 |
def split(self, availWidth, availHeight): |
|
403 | 382 |
self._calc() |
350 | 383 |
if self.splitByRow: |
384 |
if self._width>availWidth: return [] |
|
385 |
return self._splitRows(availHeight) |
|
386 |
else: |
|
387 |
raise NotImplementedError |
|
403 | 388 |
|
326 | 389 |
def draw(self): |
390 |
nudge = 0.5 * (self.availWidth - self._width) |
|
391 |
self.canv.translate(nudge, 0) |
|
392 |
self._drawBkgrnd() |
|
393 |
self._drawLines() |
|
350 | 394 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights): |
326 | 395 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
396 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
397 |
||
398 |
def _drawBkgrnd(self): |
|
399 |
for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds: |
|
400 |
if sc < 0: sc = sc + self._ncols |
|
401 |
if ec < 0: ec = ec + self._ncols |
|
402 |
if sr < 0: sr = sr + self._nrows |
|
403 |
if er < 0: er = er + self._nrows |
|
419 | 404 |
color = colors.toColor(color) |
326 | 405 |
x0 = self._colpositions[sc] |
406 |
y0 = self._rowpositions[sr] |
|
407 |
x1 = self._colpositions[ec+1] |
|
408 |
y1 = self._rowpositions[er+1] |
|
409 |
self.canv.setFillColor(color) |
|
410 |
self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1) |
|
411 |
||
412 |
def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)): |
|
413 |
if self._curcellstyle is not cellstyle: |
|
414 |
cur = self._curcellstyle |
|
415 |
if cur is None or cellstyle.color != cur.color: |
|
416 |
self.canv.setFillColor(cellstyle.color) |
|
417 |
if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize: |
|
418 |
self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
419 |
self._curcellstyle = cellstyle |
|
419 | 420 |
|
326 | 421 |
just = cellstyle.alignment |
419 | 422 |
valign = cellstyle.valign |
423 |
n = type(cellval) |
|
421 | 424 |
if n in _SeqTypes or isinstance(cellval,Flowable): |
425 |
if not n in _SeqTypes: cellval = (cellval,) |
|
419 | 426 |
# we assume it's a list of Flowables |
421 | 427 |
W = [] |
428 |
H = [] |
|
429 |
w, h = _listCellGeom(cellval,colwidth,cellstyle,W=W, H=H) |
|
419 | 430 |
if valign=='TOP': |
421 | 431 |
y = rowpos + rowheight - cellstyle.topPadding |
419 | 432 |
elif valign=='BOTTOM': |
421 | 433 |
y = rowpos+cellstyle.bottomPadding + h |
419 | 434 |
else: |
421 | 435 |
y = rowpos+(rowheight+cellstyle.bottomPadding-cellstyle.topPadding+h)/2.0 |
419 | 436 |
y = y+cellval[0].getSpaceBefore() |
421 | 437 |
for v, w, h in map(None,cellval,W,H): |
419 | 438 |
if just=='LEFT': x = colpos+cellstyle.leftPadding |
439 |
elif just=='RIGHT': x = colpos+colwidth-cellstyle.rightPadding - w |
|
421 | 440 |
elif just in ('CENTRE', 'CENTER'): |
441 |
x = colpos+(colwidth+cellstyle.leftPadding-cellstyle.rightPadding-w)/2.0 |
|
442 |
else: |
|
443 |
raise ValueError, 'Invalid justification %s' % just |
|
419 | 444 |
y = y - v.getSpaceBefore() |
421 | 445 |
y = y - h |
419 | 446 |
v.drawOn(self.canv,x,y) |
447 |
y = y - v.getSpaceAfter() |
|
326 | 448 |
else: |
419 | 449 |
if just == 'LEFT': |
450 |
draw = self.canv.drawString |
|
451 |
x = colpos + cellstyle.leftPadding |
|
452 |
elif just in ('CENTRE', 'CENTER'): |
|
453 |
draw = self.canv.drawCentredString |
|
454 |
x = colpos + colwidth * 0.5 |
|
455 |
elif just == 'RIGHT': |
|
456 |
draw = self.canv.drawRightString |
|
457 |
x = colpos + colwidth - cellstyle.rightPadding |
|
458 |
else: |
|
459 |
raise ValueError, 'Invalid justification %s' % just |
|
421 | 460 |
if n is StringType: val = cellval |
419 | 461 |
else: val = str(cellval) |
462 |
vals = string.split(val, "\n") |
|
463 |
n = len(vals) |
|
464 |
leading = cellstyle.leading |
|
465 |
fontsize = cellstyle.fontsize |
|
466 |
if valign=='BOTTOM': |
|
467 |
y = rowpos + cellstyle.bottomPadding+n*leading-fontsize |
|
468 |
elif valign=='TOP': |
|
469 |
y = rowpos + rowheight - cellstyle.topPadding - fontsize |
|
470 |
elif valign=='MIDDLE': |
|
471 |
y = rowpos + (cellstyle.bottomPadding + rowheight-cellstyle.topPadding+(n-1)*leading)/2.0 |
|
472 |
else: |
|
473 |
raise ValueError, "Bad valign: '%s'" % str(valign) |
|
329 | 474 |
|
419 | 475 |
for v in vals: |
476 |
draw(x, y, v) |
|
477 |
y = y-leading |
|
326 | 478 |
|
6 | 479 |
# for text, |
326 | 480 |
# drawCentredString(self, x, y, text) where x is center |
481 |
# drawRightString(self, x, y, text) where x is right |
|
482 |
# drawString(self, x, y, text) where x is left |
|
6 | 483 |
|
403 | 484 |
_LineOpMap = { 'GRID':'_drawGrid', |
485 |
'BOX':'_drawBox', |
|
486 |
'OUTLINE':'_drawBox', |
|
487 |
'INNERGRID':'_drawInnerGrid', |
|
488 |
'LINEBELOW':'_drawHLinesB', |
|
489 |
'LINEABOVE':'_drawHLines', |
|
490 |
'LINEBEFORE':'_drawVLines', |
|
491 |
'LINEAFTER':'_drawVLinesA', } |
|
6 | 492 |
|
403 | 493 |
LINECOMMANDS = _LineOpMap.keys() |
6 | 494 |
|
495 |
def _isLineCommand(cmd): |
|
326 | 496 |
return cmd[0] in LINECOMMANDS |
6 | 497 |
|
350 | 498 |
def _setCellStyle(cellStyles, i, j, op, values): |
499 |
new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j]) |
|
500 |
cellStyles[i][j] = new |
|
326 | 501 |
if op == 'FONT': |
357 | 502 |
n = len(values) |
326 | 503 |
new.fontname = values[0] |
357 | 504 |
if n>1: |
505 |
new.fontsize = values[1] |
|
361 | 506 |
if n>2: |
507 |
new.leading = values[2] |
|
508 |
else: |
|
509 |
new.leading = new.fontsize*1.2 |
|
357 | 510 |
elif op in ('FONTNAME', 'FACE'): |
511 |
new.fontname = values[0] |
|
512 |
elif op in ('SIZE', 'FONTSIZE'): |
|
513 |
new.fontsize = values[0] |
|
514 |
elif op == 'LEADING': |
|
515 |
new.leading = values[0] |
|
326 | 516 |
elif op == 'TEXTCOLOR': |
517 |
new.color = colors.toColor(values[0], colors.Color(0,0,0)) |
|
518 |
elif op in ('ALIGN', 'ALIGNMENT'): |
|
519 |
new.alignment = values[0] |
|
329 | 520 |
elif op == 'VALIGN': |
521 |
new.valign = values[0] |
|
326 | 522 |
elif op == 'LEFTPADDING': |
523 |
new.leftPadding = values[0] |
|
524 |
elif op == 'RIGHTPADDING': |
|
525 |
new.rightPadding = values[0] |
|
526 |
elif op == 'TOPPADDING': |
|
527 |
new.topPadding = values[0] |
|
528 |
elif op == 'BOTTOMPADDING': |
|
529 |
new.bottomPadding = values[0] |
|
6 | 530 |
|
531 |
GRID_STYLE = TableStyle( |
|
326 | 532 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
533 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
534 |
) |
|
6 | 535 |
BOX_STYLE = TableStyle( |
326 | 536 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
537 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
538 |
) |
|
6 | 539 |
LABELED_GRID_STYLE = TableStyle( |
326 | 540 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
541 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
542 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
543 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
544 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
545 |
) |
|
6 | 546 |
COLORED_GRID_STYLE = TableStyle( |
326 | 547 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
548 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
549 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
550 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
551 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
552 |
) |
|
6 | 553 |
LIST_STYLE = TableStyle( |
326 | 554 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
555 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
556 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
557 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
558 |
) |
|
6 | 559 |
|
560 |
def test(): |
|
329 | 561 |
from reportlab.lib.units import inch |
326 | 562 |
rowheights = (24, 16, 16, 16, 16) |
563 |
rowheights2 = (24, 16, 16, 16, 30) |
|
564 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
565 |
data = ( |
|
566 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
567 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
568 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
569 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
570 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
571 |
) |
|
572 |
data2 = ( |
|
573 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
574 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
575 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
576 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
577 |
('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
578 |
) |
|
579 |
styleSheet = getSampleStyleSheet() |
|
580 |
lst = [] |
|
581 |
lst.append(Paragraph("Tables", styleSheet['Heading1'])) |
|
582 |
lst.append(Paragraph(__doc__, styleSheet['BodyText'])) |
|
583 |
lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText'])) |
|
584 |
lst.append(Preformatted(""" |
|
585 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
586 |
rowheights = (24, 16, 16, 16, 16) |
|
587 |
data = ( |
|
588 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', |
|
589 |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
590 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
591 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
592 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
593 |
('Hats', 893, 912, '1,212', 643, 789, 159, |
|
594 |
888, '1,298', 832, 453, '1,344','2,843') |
|
595 |
) |
|
356 | 596 |
t = Table(data, colwidths, rowheights) |
326 | 597 |
""", styleSheet['Code'], dedent=4)) |
598 |
lst.append(Paragraph(""" |
|
599 |
You can then give the Table a TableStyle object to control its format. The first TableStyle used was |
|
600 |
created as follows: |
|
601 |
""", styleSheet['BodyText'])) |
|
602 |
lst.append(Preformatted(""" |
|
6 | 603 |
GRID_STYLE = TableStyle( |
326 | 604 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
605 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
606 |
) |
|
607 |
""", styleSheet['Code'])) |
|
608 |
lst.append(Paragraph(""" |
|
609 |
TableStyles are created by passing in a list of commands. There are two types of commands - line commands |
|
610 |
and cell formatting commands. In all cases, the first three elements of a command are the command name, |
|
611 |
the starting cell and the ending cell. |
|
612 |
""", styleSheet['BodyText'])) |
|
613 |
lst.append(Paragraph(""" |
|
614 |
Line commands always follow this with the weight and color of the desired lines. Colors can be names, |
|
615 |
or they can be specified as a (R,G,B) tuple, where R, G and B are floats and (0,0,0) is black. The line |
|
616 |
command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE |
|
617 |
and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and |
|
618 |
INNERGRID. |
|
619 |
""", styleSheet['BodyText'])) |
|
620 |
lst.append(Paragraph(""" |
|
621 |
Cell formatting commands are: |
|
622 |
""", styleSheet['BodyText'])) |
|
623 |
lst.append(Paragraph(""" |
|
624 |
FONT - takes fontname, fontsize and (optional) leading. |
|
625 |
""", styleSheet['Definition'])) |
|
626 |
lst.append(Paragraph(""" |
|
627 |
TEXTCOLOR - takes a color name or (R,G,B) tuple. |
|
628 |
""", styleSheet['Definition'])) |
|
629 |
lst.append(Paragraph(""" |
|
630 |
ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER). |
|
631 |
""", styleSheet['Definition'])) |
|
632 |
lst.append(Paragraph(""" |
|
633 |
LEFTPADDING - defaults to 6. |
|
634 |
""", styleSheet['Definition'])) |
|
635 |
lst.append(Paragraph(""" |
|
636 |
RIGHTPADDING - defaults to 6. |
|
637 |
""", styleSheet['Definition'])) |
|
638 |
lst.append(Paragraph(""" |
|
639 |
BOTTOMPADDING - defaults to 3. |
|
640 |
""", styleSheet['Definition'])) |
|
641 |
lst.append(Paragraph(""" |
|
642 |
A tablestyle is applied to a table by calling Table.setStyle(tablestyle). |
|
643 |
""", styleSheet['BodyText'])) |
|
356 | 644 |
t = Table(data, colwidths, rowheights) |
326 | 645 |
t.setStyle(GRID_STYLE) |
646 |
lst.append(PageBreak()) |
|
647 |
lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText'])) |
|
648 |
lst.append(t) |
|
649 |
||
356 | 650 |
t = Table(data, colwidths, rowheights) |
326 | 651 |
t.setStyle(BOX_STYLE) |
652 |
lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText'])) |
|
653 |
lst.append(t) |
|
654 |
lst.append(Paragraph(""" |
|
655 |
It was created as follows: |
|
656 |
""", styleSheet['BodyText'])) |
|
657 |
lst.append(Preformatted(""" |
|
6 | 658 |
BOX_STYLE = TableStyle( |
326 | 659 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
660 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
661 |
) |
|
662 |
""", styleSheet['Code'])) |
|
663 |
||
356 | 664 |
t = Table(data, colwidths, rowheights) |
326 | 665 |
t.setStyle(LABELED_GRID_STYLE) |
666 |
lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
667 |
lst.append(t) |
|
356 | 668 |
t = Table(data2, colwidths, rowheights2) |
326 | 669 |
t.setStyle(LABELED_GRID_STYLE) |
670 |
lst.append(Paragraph("This is LABELED_GRID_STYLE ILLUSTRATES EXPLICIT LINE SPLITTING WITH NEWLINE (different heights and data)\n", styleSheet['BodyText'])) |
|
671 |
lst.append(t) |
|
672 |
lst.append(Paragraph(""" |
|
673 |
It was created as follows: |
|
674 |
""", styleSheet['BodyText'])) |
|
675 |
lst.append(Preformatted(""" |
|
6 | 676 |
LABELED_GRID_STYLE = TableStyle( |
326 | 677 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
678 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
679 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
680 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
681 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
682 |
) |
|
683 |
""", styleSheet['Code'])) |
|
684 |
lst.append(PageBreak()) |
|
685 |
||
356 | 686 |
t = Table(data, colwidths, rowheights) |
326 | 687 |
t.setStyle(COLORED_GRID_STYLE) |
688 |
lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
689 |
lst.append(t) |
|
690 |
lst.append(Paragraph(""" |
|
691 |
It was created as follows: |
|
692 |
""", styleSheet['BodyText'])) |
|
693 |
lst.append(Preformatted(""" |
|
6 | 694 |
COLORED_GRID_STYLE = TableStyle( |
326 | 695 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
696 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
697 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
698 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
699 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
700 |
) |
|
701 |
""", styleSheet['Code'])) |
|
702 |
||
356 | 703 |
t = Table(data, colwidths, rowheights) |
326 | 704 |
t.setStyle(LIST_STYLE) |
705 |
lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText'])) |
|
706 |
lst.append(t) |
|
707 |
lst.append(Paragraph(""" |
|
708 |
It was created as follows: |
|
709 |
""", styleSheet['BodyText'])) |
|
710 |
lst.append(Preformatted(""" |
|
6 | 711 |
LIST_STYLE = TableStyle( |
326 | 712 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
713 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
714 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
715 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
716 |
) |
|
717 |
""", styleSheet['Code'])) |
|
6 | 718 |
|
356 | 719 |
t = Table(data, colwidths, rowheights) |
326 | 720 |
ts = TableStyle( |
721 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
|
722 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
723 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
724 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
725 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
726 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
727 |
) |
|
728 |
t.setStyle(ts) |
|
729 |
lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText'])) |
|
730 |
lst.append(t) |
|
731 |
lst.append(Paragraph(""" |
|
732 |
It was created as follows: |
|
733 |
""", styleSheet['BodyText'])) |
|
734 |
lst.append(Preformatted(""" |
|
6 | 735 |
ts = TableStyle( |
326 | 736 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
737 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
738 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
739 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
740 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
741 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
742 |
) |
|
743 |
""", styleSheet['Code'])) |
|
744 |
data = ( |
|
745 |
('', 'Jan\nCold', 'Feb\n', 'Mar\n','Apr\n','May\n', 'Jun\nHot', 'Jul\n', 'Aug\nThunder', 'Sep\n', 'Oct\n', 'Nov\n', 'Dec\n'), |
|
746 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
747 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
748 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
749 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
750 |
) |
|
751 |
c = list(colwidths) |
|
752 |
c[0] = None |
|
753 |
c[8] = None |
|
356 | 754 |
t = Table(data, c, [None]+list(rowheights[1:])) |
326 | 755 |
t.setStyle(LIST_STYLE) |
756 |
lst.append(Paragraph(""" |
|
757 |
This is a LIST_STYLE table with the first rowheight set to None ie automatic. |
|
758 |
The top row cells are split at a newline '\\n' character. The first and August |
|
759 |
column widths were also set to None. |
|
760 |
""", styleSheet['BodyText'])) |
|
761 |
lst.append(t) |
|
329 | 762 |
lst.append(Paragraph(""" |
763 |
The red numbers should be aligned LEFT & BOTTOM, the blue RIGHT & TOP |
|
764 |
and the green CENTER & MIDDLE. |
|
765 |
""", styleSheet['BodyText'])) |
|
357 | 766 |
XY = [['X00y', 'X01y', 'X02y', 'X03y', 'X04y'], |
338 | 767 |
['X10y', 'X11y', 'X12y', 'X13y', 'X14y'], |
768 |
['X20y', 'X21y', 'X22y', 'X23y', 'X24y'], |
|
769 |
['X30y', 'X31y', 'X32y', 'X33y', 'X34y']] |
|
357 | 770 |
t=Table(XY, 5*[0.6*inch], 4*[0.6*inch]) |
338 | 771 |
t.setStyle([('ALIGN',(1,1),(-2,-2),'LEFT'), |
772 |
('TEXTCOLOR',(1,1),(-2,-2),colors.red), |
|
773 |
||
774 |
('VALIGN',(0,0),(1,-1),'TOP'), |
|
775 |
('ALIGN',(0,0),(1,-1),'RIGHT'), |
|
776 |
('TEXTCOLOR',(0,0),(1,-1),colors.blue), |
|
777 |
||
778 |
('ALIGN',(0,-1),(-1,-1),'CENTER'), |
|
779 |
('VALIGN',(0,-1),(-1,-1),'MIDDLE'), |
|
780 |
('TEXTCOLOR',(0,-1),(-1,-1),colors.green), |
|
781 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
782 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
783 |
]) |
|
329 | 784 |
lst.append(t) |
333 | 785 |
data = [('alignment', 'align\012alignment'), |
786 |
('bulletColor', 'bulletcolor\012bcolor'), |
|
787 |
('bulletFontName', 'bfont\012bulletfontname'), |
|
788 |
('bulletFontSize', 'bfontsize\012bulletfontsize'), |
|
789 |
('bulletIndent', 'bindent\012bulletindent'), |
|
790 |
('firstLineIndent', 'findent\012firstlineindent'), |
|
791 |
('fontName', 'face\012fontname\012font'), |
|
792 |
('fontSize', 'size\012fontsize'), |
|
793 |
('leading', 'leading'), |
|
794 |
('leftIndent', 'leftindent\012lindent'), |
|
795 |
('rightIndent', 'rightindent\012rindent'), |
|
796 |
('spaceAfter', 'spaceafter\012spacea'), |
|
797 |
('spaceBefore', 'spacebefore\012spaceb'), |
|
798 |
('textColor', 'fg\012textcolor\012color')] |
|
356 | 799 |
t = Table(data) |
338 | 800 |
t.setStyle([ |
333 | 801 |
('VALIGN',(0,0),(-1,-1),'TOP'), |
802 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
803 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
338 | 804 |
]) |
333 | 805 |
lst.append(t) |
403 | 806 |
t = Table([ ('Attribute', 'Synonyms'), |
807 |
('alignment', 'align, alignment'), |
|
808 |
('bulletColor', 'bulletcolor, bcolor'), |
|
809 |
('bulletFontName', 'bfont, bulletfontname'), |
|
810 |
('bulletFontSize', 'bfontsize, bulletfontsize'), |
|
811 |
('bulletIndent', 'bindent, bulletindent'), |
|
812 |
('firstLineIndent', 'findent, firstlineindent'), |
|
813 |
('fontName', 'face, fontname, font'), |
|
814 |
('fontSize', 'size, fontsize'), |
|
815 |
('leading', 'leading'), |
|
816 |
('leftIndent', 'leftindent, lindent'), |
|
817 |
('rightIndent', 'rightindent, rindent'), |
|
818 |
('spaceAfter', 'spaceafter, spacea'), |
|
819 |
('spaceBefore', 'spacebefore, spaceb'), |
|
820 |
('textColor', 'fg, textcolor, color')]) |
|
350 | 821 |
t.repeatRows = 1 |
822 |
t.setStyle([ |
|
823 |
('FONT',(0,0),(-1,1),'Times-Bold',10,12), |
|
824 |
('FONT',(0,1),(-1,-1),'Courier',8,8), |
|
825 |
('VALIGN',(0,0),(-1,-1),'MIDDLE'), |
|
826 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
827 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
354 | 828 |
('BACKGROUND', (0, 0), (-1, 0), colors.green), |
829 |
('BACKGROUND', (0, 1), (-1, -1), colors.pink), |
|
830 |
('ALIGN', (0, 0), (-1, 0), 'CENTER'), |
|
831 |
('ALIGN', (0, 1), (0, -1), 'LEFT'), |
|
832 |
('ALIGN', (-1, 1), (-1, -1), 'RIGHT'), |
|
833 |
('FONT', (0, 0), (-1, 0), 'Times-Bold', 12), |
|
834 |
('ALIGN', (1, 1), (1, -1), 'CENTER'), |
|
350 | 835 |
]) |
836 |
lst.append(t) |
|
357 | 837 |
lst.append(Table(XY, |
838 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 5,6), |
|
359 | 839 |
('GRID', (0,0), (-1,-1), 0.25, colors.blue),])) |
357 | 840 |
lst.append(Table(XY, |
841 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 10,12), |
|
359 | 842 |
('GRID', (0,0), (-1,-1), 0.25, colors.black),])) |
357 | 843 |
lst.append(Table(XY, |
844 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 20,24), |
|
359 | 845 |
('GRID', (0,0), (-1,-1), 0.25, colors.red),])) |
403 | 846 |
lst.append(PageBreak()) |
847 |
data= [['00', '01', '02', '03', '04'], |
|
848 |
['10', '11', '12', '13', '14'], |
|
849 |
['20', '21', '22', '23', '24'], |
|
850 |
['30', '31', '32', '33', '34']] |
|
421 | 851 |
t=Table(data,style=[ |
852 |
('GRID',(0,0),(-1,-1),0.5,colors.grey), |
|
853 |
('GRID',(1,1),(-2,-2),1,colors.green), |
|
403 | 854 |
('BOX',(0,0),(1,-1),2,colors.red), |
421 | 855 |
('BOX',(0,0),(-1,-1),2,colors.black), |
403 | 856 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
857 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
419 | 858 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
859 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
860 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
403 | 861 |
]) |
862 |
lst.append(t) |
|
863 |
lst.append(Spacer(0,6)) |
|
864 |
for s in t.split(4*inch,30): |
|
865 |
lst.append(s) |
|
866 |
lst.append(Spacer(0,6)) |
|
867 |
lst.append(Spacer(0,6)) |
|
868 |
for s in t.split(4*inch,36): |
|
869 |
lst.append(s) |
|
870 |
lst.append(Spacer(0,6)) |
|
871 |
||
419 | 872 |
lst.append(Spacer(0,6)) |
873 |
for s in t.split(4*inch,56): |
|
874 |
lst.append(s) |
|
875 |
lst.append(Spacer(0,6)) |
|
876 |
||
421 | 877 |
import os, reportlab.platypus |
878 |
I = Image(os.path.join(os.path.dirname(reportlab.platypus.__file__),'..','demos','pythonpoint','leftlogo.gif')) |
|
879 |
I.drawHeight = 1.25*inch*I.drawHeight / I.drawWidth |
|
880 |
I.drawWidth = 1.25*inch |
|
881 |
I.noImageCaching = 1 |
|
882 |
P = Paragraph("<para align=center spaceb=3>The <b>ReportLab Left <font color=red>Logo</font></b> Image</para>", styleSheet["BodyText"]) |
|
883 |
data= [['A', 'B', 'C', Paragraph("<b>A pa<font color=red>r</font>a<i>graph</i></b><super><font color=yellow>1</font></super>",styleSheet["BodyText"]), 'D'], |
|
884 |
['00', '01', '02', [I,P], '04'], |
|
885 |
['10', '11', '12', [I,P], '14'], |
|
419 | 886 |
['20', '21', '22', '23', '24'], |
887 |
['30', '31', '32', '33', '34']] |
|
888 |
||
889 |
t=Table(data,style=[('GRID',(1,1),(-2,-2),1,colors.green), |
|
890 |
('BOX',(0,0),(1,-1),2,colors.red), |
|
891 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
|
892 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
893 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
|
894 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
895 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
896 |
('BOX',(0,0),(-1,-1),2,colors.black), |
|
897 |
('GRID',(0,0),(-1,-1),0.5,colors.black), |
|
898 |
('VALIGN',(3,0),(3,0),'BOTTOM'), |
|
899 |
('BACKGROUND',(3,0),(3,0),colors.limegreen), |
|
421 | 900 |
('BACKGROUND',(3,1),(3,1),colors.khaki), |
901 |
('ALIGN',(3,1),(3,1),'CENTER'), |
|
902 |
('BACKGROUND',(3,2),(3,2),colors.beige), |
|
903 |
('ALIGN',(3,2),(3,2),'LEFT'), |
|
419 | 904 |
]) |
905 |
||
906 |
t._argW[3]=1.5*inch |
|
907 |
lst.append(t) |
|
908 |
||
356 | 909 |
SimpleDocTemplate('tables.pdf', showBoundary=1).build(lst) |
6 | 910 |
|
911 |
if __name__ == '__main__': |
|
326 | 912 |
test() |