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