author | rgbecker |
Tue, 11 Jul 2000 14:29:45 +0000 | |
changeset 350 | 0916ae478b25 |
parent 342 | a6982189331d |
child 354 | 807513dd52b7 |
permissions | -rwxr-xr-x |
6 | 1 |
############################################################################### |
2 |
# |
|
3 |
# ReportLab Public License Version 1.0 |
|
4 |
# |
|
326 | 5 |
# Except for the change of names the spirit and intention of this |
6 |
# license is the same as that of Python |
|
6 | 7 |
# |
8 |
# (C) Copyright ReportLab Inc. 1998-2000. |
|
9 |
# |
|
10 |
# |
|
11 |
# All Rights Reserved |
|
12 |
# |
|
13 |
# Permission to use, copy, modify, and distribute this software and its |
|
14 |
# documentation for any purpose and without fee is hereby granted, provided |
|
15 |
# that the above copyright notice appear in all copies and that both that |
|
16 |
# copyright notice and this permission notice appear in supporting |
|
7 | 17 |
# documentation, and that the name of ReportLab not be used |
6 | 18 |
# in advertising or publicity pertaining to distribution of the software |
19 |
# without specific, written prior permission. |
|
20 |
# |
|
21 |
# |
|
22 |
# Disclaimer |
|
23 |
# |
|
24 |
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
|
25 |
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, |
|
26 |
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT |
|
27 |
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS |
|
28 |
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
|
29 |
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
30 |
# PERFORMANCE OF THIS SOFTWARE. |
|
31 |
# |
|
32 |
############################################################################### |
|
33 |
# $Log: tables.py,v $ |
|
350 | 34 |
# Revision 1.20 2000/07/11 14:29:45 rgbecker |
35 |
# Table splitting start |
|
36 |
# |
|
342 | 37 |
# Revision 1.19 2000/07/10 15:25:47 andy_robinson |
38 |
# Added tables to PythonPoint |
|
350 | 39 |
# |
338 | 40 |
# Revision 1.18 2000/07/08 15:30:04 rgbecker |
41 |
# Cosmetics and error testing |
|
342 | 42 |
# |
333 | 43 |
# Revision 1.17 2000/07/07 16:22:10 rgbecker |
44 |
# Fix auto hieght stuff |
|
338 | 45 |
# |
329 | 46 |
# Revision 1.16 2000/07/07 10:23:36 rgbecker |
47 |
# First attempt at VALIGN |
|
333 | 48 |
# |
329 | 49 |
# Revision 1.15 2000/07/06 14:05:55 rgbecker |
327 | 50 |
# Adjusted doc string |
329 | 51 |
# |
52 |
# Revision 1.14 2000/07/06 12:41:47 rgbecker |
|
326 | 53 |
# First try at auto sizing |
327 | 54 |
# |
326 | 55 |
# Revision 1.13 2000/06/29 17:55:19 aaron_watters |
312 | 56 |
# support explicit \n line splitting in cells |
326 | 57 |
# |
58 |
# Revision 1.12 2000/06/13 13:03:31 aaron_watters |
|
268 | 59 |
# more documentation changes |
312 | 60 |
# |
326 | 61 |
# Revision 1.11 2000/06/01 15:23:06 rgbecker |
253 | 62 |
# Platypus re-organisation |
268 | 63 |
# |
326 | 64 |
# Revision 1.10 2000/05/26 09:49:23 rgbecker |
248 | 65 |
# Color fixes; thanks to J Alet |
253 | 66 |
# |
221 | 67 |
# Revision 1.9 2000/05/16 16:15:16 rgbecker |
68 |
# Changes related to removal of SimpleFlowDocument |
|
248 | 69 |
# |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
70 |
# Revision 1.8 2000/04/26 11:07:15 andy_robinson |
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
71 |
# Tables changed to use reportlab.lib.colors instead of |
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
72 |
# the six hard-coded color strings there previously. |
221 | 73 |
# |
129 | 74 |
# Revision 1.7 2000/04/14 12:17:05 rgbecker |
75 |
# Splitting layout.py |
|
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
76 |
# |
128 | 77 |
# Revision 1.6 2000/04/14 11:54:57 rgbecker |
78 |
# Splitting layout.py |
|
129 | 79 |
# |
122 | 80 |
# Revision 1.5 2000/04/14 08:56:20 rgbecker |
81 |
# Drawable ==> Flowable |
|
128 | 82 |
# |
16 | 83 |
# Revision 1.4 2000/02/17 02:09:05 rgbecker |
84 |
# Docstring & other fixes |
|
122 | 85 |
# |
7 | 86 |
# Revision 1.3 2000/02/15 17:55:59 rgbecker |
87 |
# License text fixes |
|
16 | 88 |
# |
6 | 89 |
# Revision 1.2 2000/02/15 15:47:09 rgbecker |
90 |
# Added license, __version__ and Logi comment |
|
7 | 91 |
# |
350 | 92 |
__version__=''' $Id: tables.py,v 1.20 2000/07/11 14:29:45 rgbecker Exp $ ''' |
16 | 93 |
__doc__=""" |
6 | 94 |
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
95 |
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the |
|
96 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
268 | 97 |
|
327 | 98 |
None values in the sequence of row heights or column widths, mean that the corresponding rows |
99 |
or columns should be automatically sized. |
|
100 |
||
101 |
All the cell values should be convertible to strings; embedded newline '\\n' characters |
|
102 |
cause the value to wrap (ie are like a traditional linefeed). |
|
103 |
||
268 | 104 |
See the test output from running this module as a script for a discussion of the method for constructing |
105 |
tables and table styles. |
|
6 | 106 |
""" |
253 | 107 |
from reportlab.platypus import * |
129 | 108 |
from reportlab.lib.styles import PropertySet, getSampleStyleSheet |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
109 |
from reportlab.lib import colors |
253 | 110 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE |
312 | 111 |
import operator, string |
6 | 112 |
|
113 |
_stringtype = type('') |
|
114 |
||
128 | 115 |
class CellStyle(PropertySet): |
326 | 116 |
defaults = { |
117 |
'fontname':'Times-Roman', |
|
118 |
'fontsize':10, |
|
119 |
'leading':12, |
|
120 |
'leftPadding':6, |
|
121 |
'rightPadding':6, |
|
122 |
'topPadding':3, |
|
123 |
'bottomPadding':3, |
|
124 |
'firstLineIndent':0, |
|
125 |
'color':colors.black, |
|
126 |
'alignment': 'LEFT', |
|
127 |
'background': (1,1,1), |
|
329 | 128 |
'valign': 'BOTTOM', |
326 | 129 |
} |
6 | 130 |
|
131 |
class TableStyle: |
|
326 | 132 |
def __init__(self, cmds=None): |
133 |
self._cmds = cmds |
|
134 |
if cmds is None: |
|
135 |
self._cmds = [] |
|
136 |
def add(self, *cmd): |
|
137 |
self._cmds.append(cmd) |
|
138 |
def getCommands(self): |
|
139 |
return self._cmds |
|
338 | 140 |
|
141 |
TableStyleType = type(TableStyle()) |
|
326 | 142 |
|
221 | 143 |
class Table(Flowable): |
350 | 144 |
def __init__(self, colWidths, rowHeights, data, style=None, |
145 |
repeatRows=0, repeatCols=0, splitByRow=1): |
|
326 | 146 |
if not colWidths: |
147 |
raise ValueError, "Table must have at least 1 column" |
|
148 |
if not rowHeights: |
|
149 |
raise ValueError, "Table must have at least 1 row" |
|
150 |
nrows = self._nrows = len(rowHeights) |
|
151 |
if len(data) != nrows: |
|
152 |
raise ValueError, "Data error - %d rows in data but %d in grid" % (len(data), nrows) |
|
153 |
ncols = self._ncols = len(colWidths) |
|
154 |
for i in range(nrows): |
|
155 |
if len(data[i]) != ncols: |
|
156 |
raise ValueError, "Not enough data points in row %d!" % i |
|
350 | 157 |
self._rowHeights = self._argH = rowHeights |
158 |
self._colWidths = self._argW = colWidths |
|
326 | 159 |
self._cellvalues = data |
160 |
dflt = CellStyle('<default>') |
|
350 | 161 |
|
162 |
self._cellStyles = [None]*nrows |
|
326 | 163 |
for i in range(nrows): |
350 | 164 |
self._cellStyles[i] = [dflt]*ncols |
165 |
||
326 | 166 |
self._bkgrndcmds = [] |
167 |
self._linecmds = [] |
|
168 |
self._curweight = self._curcolor = self._curcellstyle = None |
|
350 | 169 |
self.repeatRows = repeatRows |
170 |
self.repeatCols = repeatCols |
|
171 |
self.splitByRow = splitByRow |
|
6 | 172 |
|
342 | 173 |
if style: |
174 |
self.setStyle(style) |
|
175 |
||
326 | 176 |
def _calc(self): |
333 | 177 |
|
350 | 178 |
H = self._argH |
179 |
W = self._argW |
|
6 | 180 |
|
326 | 181 |
if None in H: |
182 |
H = H[:] #make a copy as we'll change it |
|
333 | 183 |
self._rowHeights = H |
326 | 184 |
while None in H: |
185 |
i = H.index(None) |
|
186 |
V = self._cellvalues[i] |
|
350 | 187 |
S = self._cellStyles[i] |
326 | 188 |
h = 0 |
189 |
for v, s in map(None, V, S): |
|
190 |
if type(v) is not _stringtype: v = str(v) |
|
191 |
v = string.split(v, "\n") |
|
192 |
t = s.leading*len(v)+s.bottomPadding+s.topPadding |
|
193 |
if t>h: h = t #record a new maximum |
|
194 |
H[i] = h |
|
6 | 195 |
|
326 | 196 |
if None in W: |
197 |
W = W[:] |
|
198 |
self._colWidths = W |
|
199 |
while None in W: |
|
200 |
i = W.index(None) |
|
201 |
f = lambda x,i=i: operator.getitem(x,i) |
|
202 |
V = map(f,self._cellvalues) |
|
350 | 203 |
S = map(f,self._cellStyles) |
326 | 204 |
w = 0 |
205 |
for v, s in map(None, V, S): |
|
206 |
if type(v) is not _stringtype: v = str(v) |
|
207 |
v = string.split(v, "\n") |
|
208 |
t = s.leftPadding+s.rightPadding + max(map(lambda a, b=s.fontname, |
|
209 |
c=s.fontsize,d=self.canv: d.stringWidth(a,b,c), v)) |
|
210 |
if t>w: w = t #record a new maximum |
|
211 |
W[i] = w |
|
6 | 212 |
|
0 | 213 |
|
326 | 214 |
height = self._height = reduce(operator.add, H, 0) |
215 |
self._rowpositions = [height] # index 0 is actually topline; we skip when processing cells |
|
216 |
for h in H: |
|
217 |
height = height - h |
|
218 |
self._rowpositions.append(height) |
|
329 | 219 |
assert abs(height)<1e-8, 'Internal height error' |
326 | 220 |
width = 0 |
221 |
self._colpositions = [0] #index -1 is right side boundary; we skip when processing cells |
|
222 |
for w in W: |
|
223 |
width = width + w |
|
224 |
self._colpositions.append(width) |
|
225 |
self._width = width |
|
6 | 226 |
|
326 | 227 |
def setStyle(self, tblstyle): |
338 | 228 |
if type(tblstyle) is not TableStyleType: |
229 |
tblstyle = TableStyle(tblstyle) |
|
326 | 230 |
for cmd in tblstyle.getCommands(): |
350 | 231 |
self._addCommand(cmd) |
232 |
||
233 |
def _addCommand(self,cmd): |
|
234 |
if cmd[0] == 'BACKGROUND': |
|
235 |
self._bkgrndcmds.append(cmd) |
|
236 |
elif _isLineCommand(cmd): |
|
237 |
self._linecmds.append(cmd) |
|
238 |
else: |
|
239 |
(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:] |
|
240 |
if sc < 0: sc = sc + self._ncols |
|
241 |
if ec < 0: ec = ec + self._ncols |
|
242 |
if sr < 0: sr = sr + self._nrows |
|
243 |
if er < 0: er = er + self._nrows |
|
244 |
for i in range(sr, er+1): |
|
245 |
for j in range(sc, ec+1): |
|
246 |
_setCellStyle(self._cellStyles, i, j, op, values) |
|
326 | 247 |
|
248 |
def _drawLines(self): |
|
249 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
|
250 |
if sc < 0: sc = sc + self._ncols |
|
251 |
if ec < 0: ec = ec + self._ncols |
|
252 |
if sr < 0: sr = sr + self._nrows |
|
253 |
if er < 0: er = er + self._nrows |
|
254 |
if op == 'GRID': |
|
255 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
256 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
257 |
elif op in ('BOX', 'OUTLINE',): |
|
258 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
259 |
elif op == 'INNERGRID': |
|
260 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
261 |
elif op == 'LINEBELOW': |
|
262 |
self._drawHLines((sc, sr+1), (ec, er+1), weight, color) |
|
263 |
elif op == 'LINEABOVE': |
|
264 |
self._drawHLines((sc, sr), (ec, er), weight, color) |
|
265 |
elif op == 'LINEBEFORE': |
|
266 |
self._drawVLines((sc, sr), (ec, er), weight, color) |
|
267 |
elif op == 'LINEAFTER': |
|
268 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color) |
|
269 |
else: |
|
270 |
raise ValueError, "Unknown line style %s" % op |
|
271 |
self._curcolor = None |
|
248 | 272 |
|
326 | 273 |
def _drawBox(self, (sc, sr), (ec, er), weight, color): |
274 |
self._drawHLines((sc, sr), (ec, sr), weight, color) |
|
275 |
self._drawHLines((sc, er+1), (ec, er+1), weight, color) |
|
276 |
self._drawVLines((sc, sr), (sc, er), weight, color) |
|
277 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color) |
|
350 | 278 |
|
326 | 279 |
def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color): |
280 |
self._drawHLines((sc, sr+1), (ec, er), weight, color) |
|
281 |
self._drawVLines((sc+1, sr), (ec, er), weight, color) |
|
350 | 282 |
|
326 | 283 |
def _prepLine(self, weight, color): |
284 |
if color != self._curcolor: |
|
285 |
self.canv.setStrokeColor(color) |
|
286 |
self._curcolor = color |
|
287 |
if weight != self._curweight: |
|
288 |
self.canv.setLineWidth(weight) |
|
289 |
self._curweight = weight |
|
350 | 290 |
|
326 | 291 |
def _drawHLines(self, (sc, sr), (ec, er), weight, color): |
292 |
self._prepLine(weight, color) |
|
293 |
scp = self._colpositions[sc] |
|
294 |
ecp = self._colpositions[ec+1] |
|
295 |
for rowpos in self._rowpositions[sr:er+1]: |
|
296 |
self.canv.line(scp, rowpos, ecp, rowpos) |
|
350 | 297 |
|
326 | 298 |
def _drawVLines(self, (sc, sr), (ec, er), weight, color): |
299 |
self._prepLine(weight, color) |
|
300 |
srp = self._rowpositions[sr] |
|
301 |
erp = self._rowpositions[er+1] |
|
302 |
for colpos in self._colpositions[sc:ec+1]: |
|
303 |
self.canv.line(colpos, srp, colpos, erp) |
|
304 |
||
305 |
def wrap(self, availWidth, availHeight): |
|
306 |
self._calc() |
|
307 |
#nice and easy, since they are predetermined size |
|
308 |
self.availWidth = availWidth |
|
309 |
return (self._width, self._height) |
|
350 | 310 |
|
311 |
def _cr_0(self,n,cmds): |
|
312 |
for op, (sc, sr), (ec, er), weight, color in cmds: |
|
313 |
if sr>=n: continue |
|
314 |
if er>=n: er = n-1 |
|
315 |
self._addCommand((op, (sc, sr), (ec, er), weight, color)) |
|
316 |
||
317 |
def _cr_1_1(self,n,repeatRows, cmds): |
|
318 |
for op, (sc, sr), (ec, er), weight, color in cmds: |
|
319 |
if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue |
|
320 |
if sr>=repeatRows and sr<n: sr=repeatRows |
|
321 |
elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n |
|
322 |
if er>=repeatRows and er<n: er=repeatRows |
|
323 |
elif er>=repeatRows and er>=n: er=er+repeatRows-n |
|
324 |
self._addCommand((op, (sc, sr), (ec, er), weight, color)) |
|
325 |
||
326 |
def _cr_1_0(self,n,cmds): |
|
327 |
for op, (sc, sr), (ec, er), weight, color in cmds: |
|
328 |
if er>=0 and er<n: continue |
|
329 |
if sr>=0 and sr<n: sr=0 |
|
330 |
if sr>=n: sr = sr-n |
|
331 |
if er>=n: er = er-n |
|
332 |
self._addCommand((op, (sc, sr), (ec, er), weight, color)) |
|
333 |
||
334 |
def _splitRows(self,availHeight): |
|
335 |
self._calc() |
|
336 |
h = 0 |
|
337 |
n = 0 |
|
338 |
lim = len(self._rowHeights) |
|
339 |
while n<lim: |
|
340 |
hn = h + self._rowHeights[n] |
|
341 |
if hn>availHeight: break |
|
342 |
h = hn |
|
343 |
n = n + 1 |
|
344 |
||
345 |
if n<=self.repeatRows: |
|
346 |
return [] |
|
347 |
||
348 |
if n==lim: return [self] |
|
349 |
||
350 |
repeatRows = self.repeatRows |
|
351 |
repeatCols = self.repeatCols |
|
352 |
splitByRow = self.splitByRow |
|
353 |
data = self._cellvalues |
|
354 |
||
355 |
#we're going to split into two superRows |
|
356 |
R0 = Table( self._argW, self._argH[:n], data[:n], |
|
357 |
repeatRows=repeatRows, repeatCols=repeatCols, |
|
358 |
splitByRow=splitByRow) |
|
359 |
||
360 |
#copy the styles and commands |
|
361 |
R0._cellStyles = self._cellStyles[:n] |
|
362 |
R0._cr_0(n,self._linecmds) |
|
363 |
R0._cr_0(n,self._bkgrndcmds) |
|
364 |
||
365 |
if repeatRows: |
|
366 |
R1 = Table(self._argW, self._argH[:repeatRows]+self._argH[n:], |
|
367 |
data[:repeatRows]+data[n:], |
|
368 |
repeatRows=repeatRows, repeatCols=repeatCols, |
|
369 |
splitByRow=splitByRow) |
|
370 |
R1._cellStyles = self._cellStyles[:repeatRows]+self._cellStyles[n:] |
|
371 |
R1._cr_1_1(n,repeatRows,self._linecmds) |
|
372 |
R1._cr_1_1(n,repeatRows,self._bkgrndcmds) |
|
373 |
else: |
|
374 |
R1 = Table(self._argW, self._argH[n:],data[n:], |
|
375 |
repeatRows=repeatRows, repeatCols=repeatCols, |
|
376 |
splitByRow=splitByRow) |
|
377 |
R1._cellStyles = self._cellStyles[n:] |
|
378 |
R1._cr_1_0(n,self._linecmds) |
|
379 |
R1._cr_1_0(n,self._bkgrndcmds) |
|
380 |
||
381 |
return [R0,R1] |
|
382 |
||
383 |
def split(self, availWidth, availHeight): |
|
384 |
if self.splitByRow: |
|
385 |
if self._width>availWidth: return [] |
|
386 |
return self._splitRows(availHeight) |
|
387 |
else: |
|
388 |
raise NotImplementedError |
|
389 |
||
326 | 390 |
|
391 |
def draw(self): |
|
392 |
nudge = 0.5 * (self.availWidth - self._width) |
|
393 |
self.canv.translate(nudge, 0) |
|
394 |
self._drawBkgrnd() |
|
395 |
self._drawLines() |
|
350 | 396 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights): |
326 | 397 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
398 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
399 |
||
400 |
def _drawBkgrnd(self): |
|
401 |
for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds: |
|
402 |
if sc < 0: sc = sc + self._ncols |
|
403 |
if ec < 0: ec = ec + self._ncols |
|
404 |
if sr < 0: sr = sr + self._nrows |
|
405 |
if er < 0: er = er + self._nrows |
|
406 |
color = colors.toColor(color, colors.Color(1,1,1)) |
|
407 |
x0 = self._colpositions[sc] |
|
408 |
y0 = self._rowpositions[sr] |
|
409 |
x1 = self._colpositions[ec+1] |
|
410 |
y1 = self._rowpositions[er+1] |
|
411 |
self.canv.setFillColor(color) |
|
412 |
self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1) |
|
413 |
||
414 |
def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)): |
|
415 |
#print "cellstyle is ", repr(cellstyle), id(cellstyle) |
|
416 |
if self._curcellstyle is not cellstyle: |
|
417 |
cur = self._curcellstyle |
|
418 |
if cur is None or cellstyle.color != cur.color: |
|
419 |
#print "setting cell color to %s" % `cellstyle.color` |
|
420 |
self.canv.setFillColor(cellstyle.color) |
|
421 |
if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize: |
|
422 |
#print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
423 |
self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
424 |
self._curcellstyle = cellstyle |
|
425 |
#print "leading is ", cellstyle.leading, "size is", cellstyle.fontsize |
|
426 |
just = cellstyle.alignment |
|
427 |
#print "alignment is ", just |
|
428 |
if just == 'LEFT': |
|
429 |
draw = self.canv.drawString |
|
430 |
x = colpos + cellstyle.leftPadding |
|
431 |
elif just in ('CENTRE', 'CENTER'): |
|
432 |
draw = self.canv.drawCentredString |
|
433 |
x = colpos + colwidth * 0.5 |
|
338 | 434 |
elif just == 'RIGHT': |
326 | 435 |
draw = self.canv.drawRightString |
436 |
x = colpos + colwidth - cellstyle.rightPadding |
|
338 | 437 |
else: |
438 |
raise ValueError, 'Invalid justification %s' % just |
|
439 |
||
326 | 440 |
if type(cellval) is _stringtype: |
441 |
val = cellval |
|
442 |
else: |
|
443 |
val = str(cellval) |
|
444 |
vals = string.split(val, "\n") |
|
445 |
n = len(vals)-1 |
|
446 |
leading = cellstyle.leading |
|
329 | 447 |
valign = cellstyle.valign |
448 |
if valign=='BOTTOM': |
|
449 |
y = rowpos + cellstyle.bottomPadding+n*leading |
|
450 |
elif valign=='TOP': |
|
338 | 451 |
y = rowpos + rowheight - cellstyle.topPadding - cellstyle.fontsize |
329 | 452 |
elif valign=='MIDDLE': |
453 |
y = rowpos + (cellstyle.bottomPadding + rowheight - cellstyle.topPadding+n*leading)/2.0 |
|
454 |
else: |
|
455 |
raise ValueError, "Bad valign: '%s'" % str(valign) |
|
456 |
||
326 | 457 |
for v in vals: |
329 | 458 |
draw(x, y, v) |
459 |
y = y-leading |
|
326 | 460 |
|
6 | 461 |
# for text, |
326 | 462 |
# drawCentredString(self, x, y, text) where x is center |
463 |
# drawRightString(self, x, y, text) where x is right |
|
464 |
# drawString(self, x, y, text) where x is left |
|
6 | 465 |
|
466 |
LINECOMMANDS = ( |
|
326 | 467 |
'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', ) |
6 | 468 |
|
469 |
||
470 |
def _isLineCommand(cmd): |
|
326 | 471 |
return cmd[0] in LINECOMMANDS |
6 | 472 |
|
350 | 473 |
def _setCellStyle(cellStyles, i, j, op, values): |
474 |
new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j]) |
|
475 |
cellStyles[i][j] = new |
|
326 | 476 |
if op == 'FONT': |
477 |
new.fontname = values[0] |
|
478 |
new.fontsize = values[1] |
|
479 |
elif op == 'TEXTCOLOR': |
|
480 |
new.color = colors.toColor(values[0], colors.Color(0,0,0)) |
|
481 |
elif op in ('ALIGN', 'ALIGNMENT'): |
|
482 |
new.alignment = values[0] |
|
329 | 483 |
elif op == 'VALIGN': |
484 |
new.valign = values[0] |
|
326 | 485 |
elif op == 'LEFTPADDING': |
486 |
new.leftPadding = values[0] |
|
487 |
elif op == 'RIGHTPADDING': |
|
488 |
new.rightPadding = values[0] |
|
489 |
elif op == 'TOPPADDING': |
|
490 |
new.topPadding = values[0] |
|
491 |
elif op == 'BOTTOMPADDING': |
|
492 |
new.bottomPadding = values[0] |
|
6 | 493 |
|
494 |
GRID_STYLE = TableStyle( |
|
326 | 495 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
496 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
497 |
) |
|
6 | 498 |
BOX_STYLE = TableStyle( |
326 | 499 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
500 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
501 |
) |
|
6 | 502 |
LABELED_GRID_STYLE = TableStyle( |
326 | 503 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
504 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
505 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
506 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
507 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
508 |
) |
|
6 | 509 |
COLORED_GRID_STYLE = TableStyle( |
326 | 510 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
511 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
512 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
513 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
514 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
515 |
) |
|
6 | 516 |
LIST_STYLE = TableStyle( |
326 | 517 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
518 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
519 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
520 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
521 |
) |
|
6 | 522 |
|
523 |
def test(): |
|
329 | 524 |
from reportlab.lib.units import inch |
326 | 525 |
rowheights = (24, 16, 16, 16, 16) |
526 |
rowheights2 = (24, 16, 16, 16, 30) |
|
527 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
528 |
data = ( |
|
529 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
530 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
531 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
532 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
533 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
534 |
) |
|
535 |
data2 = ( |
|
536 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
537 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
538 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
539 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
540 |
('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
541 |
) |
|
542 |
styleSheet = getSampleStyleSheet() |
|
543 |
lst = [] |
|
544 |
lst.append(Paragraph("Tables", styleSheet['Heading1'])) |
|
545 |
lst.append(Paragraph(__doc__, styleSheet['BodyText'])) |
|
546 |
lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText'])) |
|
547 |
lst.append(Preformatted(""" |
|
548 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
549 |
rowheights = (24, 16, 16, 16, 16) |
|
550 |
data = ( |
|
551 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', |
|
552 |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
553 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
554 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
555 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
556 |
('Hats', 893, 912, '1,212', 643, 789, 159, |
|
557 |
888, '1,298', 832, 453, '1,344','2,843') |
|
558 |
) |
|
559 |
t = Table(colwidths, rowheights, data) |
|
560 |
""", styleSheet['Code'], dedent=4)) |
|
561 |
lst.append(Paragraph(""" |
|
562 |
You can then give the Table a TableStyle object to control its format. The first TableStyle used was |
|
563 |
created as follows: |
|
564 |
""", styleSheet['BodyText'])) |
|
565 |
lst.append(Preformatted(""" |
|
6 | 566 |
GRID_STYLE = TableStyle( |
326 | 567 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
568 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
569 |
) |
|
570 |
""", styleSheet['Code'])) |
|
571 |
lst.append(Paragraph(""" |
|
572 |
TableStyles are created by passing in a list of commands. There are two types of commands - line commands |
|
573 |
and cell formatting commands. In all cases, the first three elements of a command are the command name, |
|
574 |
the starting cell and the ending cell. |
|
575 |
""", styleSheet['BodyText'])) |
|
576 |
lst.append(Paragraph(""" |
|
577 |
Line commands always follow this with the weight and color of the desired lines. Colors can be names, |
|
578 |
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 |
|
579 |
command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE |
|
580 |
and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and |
|
581 |
INNERGRID. |
|
582 |
""", styleSheet['BodyText'])) |
|
583 |
lst.append(Paragraph(""" |
|
584 |
Cell formatting commands are: |
|
585 |
""", styleSheet['BodyText'])) |
|
586 |
lst.append(Paragraph(""" |
|
587 |
FONT - takes fontname, fontsize and (optional) leading. |
|
588 |
""", styleSheet['Definition'])) |
|
589 |
lst.append(Paragraph(""" |
|
590 |
TEXTCOLOR - takes a color name or (R,G,B) tuple. |
|
591 |
""", styleSheet['Definition'])) |
|
592 |
lst.append(Paragraph(""" |
|
593 |
ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER). |
|
594 |
""", styleSheet['Definition'])) |
|
595 |
lst.append(Paragraph(""" |
|
596 |
LEFTPADDING - defaults to 6. |
|
597 |
""", styleSheet['Definition'])) |
|
598 |
lst.append(Paragraph(""" |
|
599 |
RIGHTPADDING - defaults to 6. |
|
600 |
""", styleSheet['Definition'])) |
|
601 |
lst.append(Paragraph(""" |
|
602 |
BOTTOMPADDING - defaults to 3. |
|
603 |
""", styleSheet['Definition'])) |
|
604 |
lst.append(Paragraph(""" |
|
605 |
A tablestyle is applied to a table by calling Table.setStyle(tablestyle). |
|
606 |
""", styleSheet['BodyText'])) |
|
607 |
t = Table(colwidths, rowheights, data) |
|
608 |
t.setStyle(GRID_STYLE) |
|
609 |
lst.append(PageBreak()) |
|
610 |
lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText'])) |
|
611 |
lst.append(t) |
|
612 |
||
613 |
t = Table(colwidths, rowheights, data) |
|
614 |
t.setStyle(BOX_STYLE) |
|
615 |
lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText'])) |
|
616 |
lst.append(t) |
|
617 |
lst.append(Paragraph(""" |
|
618 |
It was created as follows: |
|
619 |
""", styleSheet['BodyText'])) |
|
620 |
lst.append(Preformatted(""" |
|
6 | 621 |
BOX_STYLE = TableStyle( |
326 | 622 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
623 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
624 |
) |
|
625 |
""", styleSheet['Code'])) |
|
626 |
||
627 |
t = Table(colwidths, rowheights, data) |
|
628 |
t.setStyle(LABELED_GRID_STYLE) |
|
629 |
lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
630 |
lst.append(t) |
|
631 |
t = Table(colwidths, rowheights2, data2) |
|
632 |
t.setStyle(LABELED_GRID_STYLE) |
|
633 |
lst.append(Paragraph("This is LABELED_GRID_STYLE ILLUSTRATES EXPLICIT LINE SPLITTING WITH NEWLINE (different heights and data)\n", styleSheet['BodyText'])) |
|
634 |
lst.append(t) |
|
635 |
lst.append(Paragraph(""" |
|
636 |
It was created as follows: |
|
637 |
""", styleSheet['BodyText'])) |
|
638 |
lst.append(Preformatted(""" |
|
6 | 639 |
LABELED_GRID_STYLE = TableStyle( |
326 | 640 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
641 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
642 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
643 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
644 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
645 |
) |
|
646 |
""", styleSheet['Code'])) |
|
647 |
lst.append(PageBreak()) |
|
648 |
||
649 |
t = Table(colwidths, rowheights, data) |
|
650 |
t.setStyle(COLORED_GRID_STYLE) |
|
651 |
lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
652 |
lst.append(t) |
|
653 |
lst.append(Paragraph(""" |
|
654 |
It was created as follows: |
|
655 |
""", styleSheet['BodyText'])) |
|
656 |
lst.append(Preformatted(""" |
|
6 | 657 |
COLORED_GRID_STYLE = TableStyle( |
326 | 658 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
659 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
660 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
661 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
662 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
663 |
) |
|
664 |
""", styleSheet['Code'])) |
|
665 |
||
666 |
t = Table(colwidths, rowheights, data) |
|
667 |
t.setStyle(LIST_STYLE) |
|
668 |
lst.append(Paragraph("This is LIST_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 |
LIST_STYLE = TableStyle( |
326 | 675 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
676 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
677 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
678 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
679 |
) |
|
680 |
""", styleSheet['Code'])) |
|
6 | 681 |
|
326 | 682 |
t = Table(colwidths, rowheights, data) |
683 |
ts = TableStyle( |
|
684 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
|
685 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
686 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
687 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
688 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
689 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
690 |
) |
|
691 |
t.setStyle(ts) |
|
692 |
lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText'])) |
|
693 |
lst.append(t) |
|
694 |
lst.append(Paragraph(""" |
|
695 |
It was created as follows: |
|
696 |
""", styleSheet['BodyText'])) |
|
697 |
lst.append(Preformatted(""" |
|
6 | 698 |
ts = TableStyle( |
326 | 699 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
700 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
701 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
702 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
703 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
704 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
705 |
) |
|
706 |
""", styleSheet['Code'])) |
|
707 |
data = ( |
|
708 |
('', 'Jan\nCold', 'Feb\n', 'Mar\n','Apr\n','May\n', 'Jun\nHot', 'Jul\n', 'Aug\nThunder', 'Sep\n', 'Oct\n', 'Nov\n', 'Dec\n'), |
|
709 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
710 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
711 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
712 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
713 |
) |
|
714 |
c = list(colwidths) |
|
715 |
c[0] = None |
|
716 |
c[8] = None |
|
717 |
t = Table(c, [None]+list(rowheights[1:]), data) |
|
718 |
t.setStyle(LIST_STYLE) |
|
719 |
lst.append(Paragraph(""" |
|
720 |
This is a LIST_STYLE table with the first rowheight set to None ie automatic. |
|
721 |
The top row cells are split at a newline '\\n' character. The first and August |
|
722 |
column widths were also set to None. |
|
723 |
""", styleSheet['BodyText'])) |
|
724 |
lst.append(t) |
|
329 | 725 |
lst.append(Paragraph(""" |
726 |
The red numbers should be aligned LEFT & BOTTOM, the blue RIGHT & TOP |
|
727 |
and the green CENTER & MIDDLE. |
|
728 |
""", styleSheet['BodyText'])) |
|
338 | 729 |
data= [['X00y', 'X01y', 'X02y', 'X03y', 'X04y'], |
730 |
['X10y', 'X11y', 'X12y', 'X13y', 'X14y'], |
|
731 |
['X20y', 'X21y', 'X22y', 'X23y', 'X24y'], |
|
732 |
['X30y', 'X31y', 'X32y', 'X33y', 'X34y']] |
|
329 | 733 |
t=Table(5*[0.4*inch], 4*[0.4*inch], data) |
338 | 734 |
t.setStyle([('ALIGN',(1,1),(-2,-2),'LEFT'), |
735 |
('TEXTCOLOR',(1,1),(-2,-2),colors.red), |
|
736 |
||
737 |
('VALIGN',(0,0),(1,-1),'TOP'), |
|
738 |
('ALIGN',(0,0),(1,-1),'RIGHT'), |
|
739 |
('TEXTCOLOR',(0,0),(1,-1),colors.blue), |
|
740 |
||
741 |
('ALIGN',(0,-1),(-1,-1),'CENTER'), |
|
742 |
('VALIGN',(0,-1),(-1,-1),'MIDDLE'), |
|
743 |
('TEXTCOLOR',(0,-1),(-1,-1),colors.green), |
|
744 |
||
745 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
746 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
747 |
]) |
|
329 | 748 |
lst.append(t) |
333 | 749 |
data = [('alignment', 'align\012alignment'), |
750 |
('bulletColor', 'bulletcolor\012bcolor'), |
|
751 |
('bulletFontName', 'bfont\012bulletfontname'), |
|
752 |
('bulletFontSize', 'bfontsize\012bulletfontsize'), |
|
753 |
('bulletIndent', 'bindent\012bulletindent'), |
|
754 |
('firstLineIndent', 'findent\012firstlineindent'), |
|
755 |
('fontName', 'face\012fontname\012font'), |
|
756 |
('fontSize', 'size\012fontsize'), |
|
757 |
('leading', 'leading'), |
|
758 |
('leftIndent', 'leftindent\012lindent'), |
|
759 |
('rightIndent', 'rightindent\012rindent'), |
|
760 |
('spaceAfter', 'spaceafter\012spacea'), |
|
761 |
('spaceBefore', 'spacebefore\012spaceb'), |
|
762 |
('textColor', 'fg\012textcolor\012color')] |
|
763 |
t = Table(2*[None], len(data)*[None], data) |
|
338 | 764 |
t.setStyle([ |
333 | 765 |
('VALIGN',(0,0),(-1,-1),'TOP'), |
766 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
767 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
338 | 768 |
]) |
333 | 769 |
lst.append(t) |
350 | 770 |
t=apply(Table,([None, None], [None, None, None, None, None, None, None, None, None, None, None, None, None, None, None], [('Attribute', 'Synonyms'), ('alignment', 'align, alignment'), ('bulletColor', 'bulletcolor, bcolor'), ('bulletFontName', 'bfont, bulletfontname'), ('bulletFontSize', 'bfontsize, bulletfontsize'), ('bulletIndent', 'bindent, bulletindent'), ('firstLineIndent', 'findent, firstlineindent'), ('fontName', 'face, fontname, font'), ('fontSize', 'size, fontsize'), ('leading', 'leading'), ('leftIndent', 'leftindent, lindent'), ('rightIndent', 'rightindent, rindent'), ('spaceAfter', 'spaceafter, spacea'), ('spaceBefore', 'spacebefore, spaceb'), ('textColor', 'fg, textcolor, color')])) |
771 |
t.repeatRows = 1 |
|
772 |
t.setStyle([ |
|
773 |
('FONT',(0,0),(-1,1),'Times-Bold',10,12), |
|
774 |
('FONT',(0,1),(-1,-1),'Courier',8,8), |
|
775 |
('VALIGN',(0,0),(-1,-1),'MIDDLE'), |
|
776 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
777 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
778 |
]) |
|
779 |
lst.append(t) |
|
326 | 780 |
SimpleDocTemplate('testtables.pdf', showBoundary=1).build(lst) |
6 | 781 |
|
782 |
if __name__ == '__main__': |
|
326 | 783 |
test() |