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