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