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