author | rgbecker |
Thu, 06 Jul 2000 12:41:47 +0000 | |
changeset 326 | 159576a5816e |
parent 312 | b39b1a2ed9d5 |
child 327 | 0944cd1f3f80 |
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 $ |
|
326 | 34 |
# Revision 1.14 2000/07/06 12:41:47 rgbecker |
35 |
# First try at auto sizing |
|
36 |
# |
|
37 |
# Revision 1.13 2000/06/29 17:55:19 aaron_watters |
|
312 | 38 |
# support explicit \n line splitting in cells |
326 | 39 |
# |
40 |
# Revision 1.12 2000/06/13 13:03:31 aaron_watters |
|
268 | 41 |
# more documentation changes |
312 | 42 |
# |
326 | 43 |
# Revision 1.11 2000/06/01 15:23:06 rgbecker |
253 | 44 |
# Platypus re-organisation |
268 | 45 |
# |
326 | 46 |
# Revision 1.10 2000/05/26 09:49:23 rgbecker |
248 | 47 |
# Color fixes; thanks to J Alet |
253 | 48 |
# |
221 | 49 |
# Revision 1.9 2000/05/16 16:15:16 rgbecker |
50 |
# Changes related to removal of SimpleFlowDocument |
|
248 | 51 |
# |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
52 |
# 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
|
53 |
# 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
|
54 |
# the six hard-coded color strings there previously. |
221 | 55 |
# |
129 | 56 |
# Revision 1.7 2000/04/14 12:17:05 rgbecker |
57 |
# Splitting layout.py |
|
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
58 |
# |
128 | 59 |
# Revision 1.6 2000/04/14 11:54:57 rgbecker |
60 |
# Splitting layout.py |
|
129 | 61 |
# |
122 | 62 |
# Revision 1.5 2000/04/14 08:56:20 rgbecker |
63 |
# Drawable ==> Flowable |
|
128 | 64 |
# |
16 | 65 |
# Revision 1.4 2000/02/17 02:09:05 rgbecker |
66 |
# Docstring & other fixes |
|
122 | 67 |
# |
7 | 68 |
# Revision 1.3 2000/02/15 17:55:59 rgbecker |
69 |
# License text fixes |
|
16 | 70 |
# |
6 | 71 |
# Revision 1.2 2000/02/15 15:47:09 rgbecker |
72 |
# Added license, __version__ and Logi comment |
|
7 | 73 |
# |
326 | 74 |
__version__=''' $Id: tables.py,v 1.14 2000/07/06 12:41:47 rgbecker Exp $ ''' |
16 | 75 |
__doc__=""" |
6 | 76 |
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
77 |
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the |
|
78 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
268 | 79 |
|
80 |
See the test output from running this module as a script for a discussion of the method for constructing |
|
81 |
tables and table styles. |
|
6 | 82 |
""" |
253 | 83 |
from reportlab.platypus import * |
129 | 84 |
from reportlab.lib.styles import PropertySet, getSampleStyleSheet |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
85 |
from reportlab.lib import colors |
253 | 86 |
from reportlab.lib.pagesizes import DEFAULT_PAGE_SIZE |
312 | 87 |
import operator, string |
6 | 88 |
|
89 |
_stringtype = type('') |
|
90 |
||
128 | 91 |
class CellStyle(PropertySet): |
326 | 92 |
defaults = { |
93 |
'fontname':'Times-Roman', |
|
94 |
'fontsize':10, |
|
95 |
'leading':12, |
|
96 |
'leftPadding':6, |
|
97 |
'rightPadding':6, |
|
98 |
'topPadding':3, |
|
99 |
'bottomPadding':3, |
|
100 |
'firstLineIndent':0, |
|
101 |
'color':colors.black, |
|
102 |
'alignment': 'LEFT', |
|
103 |
'background': (1,1,1), |
|
104 |
} |
|
6 | 105 |
|
106 |
class TableStyle: |
|
326 | 107 |
def __init__(self, cmds=None): |
108 |
self._cmds = cmds |
|
109 |
if cmds is None: |
|
110 |
self._cmds = [] |
|
111 |
def add(self, *cmd): |
|
112 |
self._cmds.append(cmd) |
|
113 |
def getCommands(self): |
|
114 |
return self._cmds |
|
115 |
||
221 | 116 |
class Table(Flowable): |
326 | 117 |
def __init__(self, colWidths, rowHeights, data): |
118 |
if not colWidths: |
|
119 |
raise ValueError, "Table must have at least 1 column" |
|
120 |
if not rowHeights: |
|
121 |
raise ValueError, "Table must have at least 1 row" |
|
122 |
nrows = self._nrows = len(rowHeights) |
|
123 |
if len(data) != nrows: |
|
124 |
raise ValueError, "Data error - %d rows in data but %d in grid" % (len(data), nrows) |
|
125 |
ncols = self._ncols = len(colWidths) |
|
126 |
for i in range(nrows): |
|
127 |
if len(data[i]) != ncols: |
|
128 |
raise ValueError, "Not enough data points in row %d!" % i |
|
129 |
self._rowHeights = rowHeights |
|
130 |
self._colWidths = colWidths |
|
131 |
self._cellvalues = data |
|
132 |
dflt = CellStyle('<default>') |
|
133 |
self._cellstyles = [None]*nrows |
|
134 |
for i in range(nrows): |
|
135 |
self._cellstyles[i] = [dflt]*ncols |
|
136 |
self._bkgrndcmds = [] |
|
137 |
self._linecmds = [] |
|
138 |
self._curweight = self._curcolor = self._curcellstyle = None |
|
6 | 139 |
|
326 | 140 |
def _calc(self): |
141 |
H = self._rowHeights |
|
142 |
W = self._colWidths |
|
6 | 143 |
|
326 | 144 |
if None in H: |
145 |
H = H[:] #make a copy as we'll change it |
|
146 |
self._rowHeghts = H |
|
147 |
while None in H: |
|
148 |
i = H.index(None) |
|
149 |
V = self._cellvalues[i] |
|
150 |
S = self._cellstyles[i] |
|
151 |
h = 0 |
|
152 |
for v, s in map(None, V, S): |
|
153 |
if type(v) is not _stringtype: v = str(v) |
|
154 |
v = string.split(v, "\n") |
|
155 |
t = s.leading*len(v)+s.bottomPadding+s.topPadding |
|
156 |
if t>h: h = t #record a new maximum |
|
157 |
H[i] = h |
|
6 | 158 |
|
326 | 159 |
if None in W: |
160 |
W = W[:] |
|
161 |
self._colWidths = W |
|
162 |
while None in W: |
|
163 |
i = W.index(None) |
|
164 |
f = lambda x,i=i: operator.getitem(x,i) |
|
165 |
V = map(f,self._cellvalues) |
|
166 |
S = map(f,self._cellstyles) |
|
167 |
w = 0 |
|
168 |
for v, s in map(None, V, S): |
|
169 |
if type(v) is not _stringtype: v = str(v) |
|
170 |
v = string.split(v, "\n") |
|
171 |
t = s.leftPadding+s.rightPadding + max(map(lambda a, b=s.fontname, |
|
172 |
c=s.fontsize,d=self.canv: d.stringWidth(a,b,c), v)) |
|
173 |
if t>w: w = t #record a new maximum |
|
174 |
W[i] = w |
|
6 | 175 |
|
0 | 176 |
|
326 | 177 |
height = self._height = reduce(operator.add, H, 0) |
178 |
self._rowpositions = [height] # index 0 is actually topline; we skip when processing cells |
|
179 |
for h in H: |
|
180 |
height = height - h |
|
181 |
self._rowpositions.append(height) |
|
182 |
assert height == 0 |
|
183 |
width = 0 |
|
184 |
self._colpositions = [0] #index -1 is right side boundary; we skip when processing cells |
|
185 |
for w in W: |
|
186 |
width = width + w |
|
187 |
self._colpositions.append(width) |
|
188 |
self._width = width |
|
6 | 189 |
|
326 | 190 |
def setStyle(self, tblstyle): |
191 |
for cmd in tblstyle.getCommands(): |
|
192 |
if cmd[0] == 'BACKGROUND': |
|
193 |
self._bkgrndcmds.append(cmd) |
|
194 |
elif _isLineCommand(cmd): |
|
195 |
self._linecmds.append(cmd) |
|
196 |
else: |
|
197 |
(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:] |
|
198 |
if sc < 0: sc = sc + self._ncols |
|
199 |
if ec < 0: ec = ec + self._ncols |
|
200 |
if sr < 0: sr = sr + self._nrows |
|
201 |
if er < 0: er = er + self._nrows |
|
202 |
for i in range(sr, er+1): |
|
203 |
for j in range(sc, ec+1): |
|
204 |
_setCellStyle(self._cellstyles, i, j, op, values) |
|
205 |
||
206 |
def _drawLines(self): |
|
207 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
|
208 |
if sc < 0: sc = sc + self._ncols |
|
209 |
if ec < 0: ec = ec + self._ncols |
|
210 |
if sr < 0: sr = sr + self._nrows |
|
211 |
if er < 0: er = er + self._nrows |
|
212 |
if op == 'GRID': |
|
213 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
214 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
215 |
elif op in ('BOX', 'OUTLINE',): |
|
216 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
217 |
elif op == 'INNERGRID': |
|
218 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
219 |
elif op == 'LINEBELOW': |
|
220 |
self._drawHLines((sc, sr+1), (ec, er+1), weight, color) |
|
221 |
elif op == 'LINEABOVE': |
|
222 |
self._drawHLines((sc, sr), (ec, er), weight, color) |
|
223 |
elif op == 'LINEBEFORE': |
|
224 |
self._drawVLines((sc, sr), (ec, er), weight, color) |
|
225 |
elif op == 'LINEAFTER': |
|
226 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color) |
|
227 |
else: |
|
228 |
raise ValueError, "Unknown line style %s" % op |
|
229 |
self._curcolor = None |
|
248 | 230 |
|
326 | 231 |
def _drawBox(self, (sc, sr), (ec, er), weight, color): |
232 |
self._drawHLines((sc, sr), (ec, sr), weight, color) |
|
233 |
self._drawHLines((sc, er+1), (ec, er+1), weight, color) |
|
234 |
self._drawVLines((sc, sr), (sc, er), weight, color) |
|
235 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color) |
|
236 |
def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color): |
|
237 |
self._drawHLines((sc, sr+1), (ec, er), weight, color) |
|
238 |
self._drawVLines((sc+1, sr), (ec, er), weight, color) |
|
239 |
def _prepLine(self, weight, color): |
|
240 |
if color != self._curcolor: |
|
241 |
self.canv.setStrokeColor(color) |
|
242 |
self._curcolor = color |
|
243 |
if weight != self._curweight: |
|
244 |
self.canv.setLineWidth(weight) |
|
245 |
self._curweight = weight |
|
246 |
def _drawHLines(self, (sc, sr), (ec, er), weight, color): |
|
247 |
self._prepLine(weight, color) |
|
248 |
scp = self._colpositions[sc] |
|
249 |
ecp = self._colpositions[ec+1] |
|
250 |
for rowpos in self._rowpositions[sr:er+1]: |
|
251 |
self.canv.line(scp, rowpos, ecp, rowpos) |
|
252 |
def _drawVLines(self, (sc, sr), (ec, er), weight, color): |
|
253 |
self._prepLine(weight, color) |
|
254 |
srp = self._rowpositions[sr] |
|
255 |
erp = self._rowpositions[er+1] |
|
256 |
for colpos in self._colpositions[sc:ec+1]: |
|
257 |
self.canv.line(colpos, srp, colpos, erp) |
|
258 |
||
259 |
def wrap(self, availWidth, availHeight): |
|
260 |
self._calc() |
|
261 |
#nice and easy, since they are predetermined size |
|
262 |
self.availWidth = availWidth |
|
263 |
return (self._width, self._height) |
|
264 |
||
265 |
def draw(self): |
|
266 |
nudge = 0.5 * (self.availWidth - self._width) |
|
267 |
self.canv.translate(nudge, 0) |
|
268 |
self._drawBkgrnd() |
|
269 |
self._drawLines() |
|
270 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellstyles, self._rowpositions[1:], self._rowHeights): |
|
271 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
|
272 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
273 |
||
274 |
def _drawBkgrnd(self): |
|
275 |
for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds: |
|
276 |
if sc < 0: sc = sc + self._ncols |
|
277 |
if ec < 0: ec = ec + self._ncols |
|
278 |
if sr < 0: sr = sr + self._nrows |
|
279 |
if er < 0: er = er + self._nrows |
|
280 |
color = colors.toColor(color, colors.Color(1,1,1)) |
|
281 |
x0 = self._colpositions[sc] |
|
282 |
y0 = self._rowpositions[sr] |
|
283 |
x1 = self._colpositions[ec+1] |
|
284 |
y1 = self._rowpositions[er+1] |
|
285 |
self.canv.setFillColor(color) |
|
286 |
self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1) |
|
287 |
||
288 |
def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)): |
|
289 |
#print "cellstyle is ", repr(cellstyle), id(cellstyle) |
|
290 |
if self._curcellstyle is not cellstyle: |
|
291 |
cur = self._curcellstyle |
|
292 |
if cur is None or cellstyle.color != cur.color: |
|
293 |
#print "setting cell color to %s" % `cellstyle.color` |
|
294 |
self.canv.setFillColor(cellstyle.color) |
|
295 |
if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize: |
|
296 |
#print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
297 |
self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
298 |
self._curcellstyle = cellstyle |
|
299 |
#print "leading is ", cellstyle.leading, "size is", cellstyle.fontsize |
|
300 |
just = cellstyle.alignment |
|
301 |
#print "alignment is ", just |
|
302 |
if just == 'LEFT': |
|
303 |
draw = self.canv.drawString |
|
304 |
x = colpos + cellstyle.leftPadding |
|
305 |
elif just in ('CENTRE', 'CENTER'): |
|
306 |
draw = self.canv.drawCentredString |
|
307 |
x = colpos + colwidth * 0.5 |
|
308 |
else: |
|
309 |
draw = self.canv.drawRightString |
|
310 |
x = colpos + colwidth - cellstyle.rightPadding |
|
311 |
y = rowpos + cellstyle.bottomPadding |
|
312 |
if type(cellval) is _stringtype: |
|
313 |
val = cellval |
|
314 |
else: |
|
315 |
val = str(cellval) |
|
316 |
vals = string.split(val, "\n") |
|
317 |
n = len(vals)-1 |
|
318 |
leading = cellstyle.leading |
|
319 |
y2 = y+leading*n |
|
320 |
for v in vals: |
|
321 |
draw(x, y2, v) |
|
322 |
y2 = y2-leading |
|
323 |
||
6 | 324 |
# for text, |
326 | 325 |
# drawCentredString(self, x, y, text) where x is center |
326 |
# drawRightString(self, x, y, text) where x is right |
|
327 |
# drawString(self, x, y, text) where x is left |
|
6 | 328 |
|
329 |
LINECOMMANDS = ( |
|
326 | 330 |
'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', ) |
6 | 331 |
|
332 |
||
333 |
def _isLineCommand(cmd): |
|
326 | 334 |
return cmd[0] in LINECOMMANDS |
6 | 335 |
|
336 |
def _setCellStyle(cellstyles, i, j, op, values): |
|
326 | 337 |
new = CellStyle('<%d, %d>' % (i,j), cellstyles[i][j]) |
338 |
cellstyles[i][j] = new |
|
339 |
if op == 'FONT': |
|
340 |
new.fontname = values[0] |
|
341 |
new.fontsize = values[1] |
|
342 |
elif op == 'TEXTCOLOR': |
|
343 |
new.color = colors.toColor(values[0], colors.Color(0,0,0)) |
|
344 |
elif op in ('ALIGN', 'ALIGNMENT'): |
|
345 |
new.alignment = values[0] |
|
346 |
elif op == 'LEFTPADDING': |
|
347 |
new.leftPadding = values[0] |
|
348 |
elif op == 'RIGHTPADDING': |
|
349 |
new.rightPadding = values[0] |
|
350 |
elif op == 'TOPPADDING': |
|
351 |
new.topPadding = values[0] |
|
352 |
elif op == 'BOTTOMPADDING': |
|
353 |
new.bottomPadding = values[0] |
|
6 | 354 |
|
355 |
GRID_STYLE = TableStyle( |
|
326 | 356 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
357 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
358 |
) |
|
6 | 359 |
BOX_STYLE = TableStyle( |
326 | 360 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
361 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
362 |
) |
|
6 | 363 |
LABELED_GRID_STYLE = TableStyle( |
326 | 364 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
365 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
366 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
367 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
368 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
369 |
) |
|
6 | 370 |
COLORED_GRID_STYLE = TableStyle( |
326 | 371 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
372 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
373 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
374 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
375 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
376 |
) |
|
6 | 377 |
LIST_STYLE = TableStyle( |
326 | 378 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
379 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
380 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
381 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
382 |
) |
|
6 | 383 |
|
384 |
def test(): |
|
326 | 385 |
rowheights = (24, 16, 16, 16, 16) |
386 |
rowheights2 = (24, 16, 16, 16, 30) |
|
387 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
388 |
data = ( |
|
389 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
390 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
391 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
392 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
393 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
394 |
) |
|
395 |
data2 = ( |
|
396 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
397 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
398 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
399 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
400 |
('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
401 |
) |
|
402 |
styleSheet = getSampleStyleSheet() |
|
403 |
lst = [] |
|
404 |
lst.append(Paragraph("Tables", styleSheet['Heading1'])) |
|
405 |
lst.append(Paragraph(__doc__, styleSheet['BodyText'])) |
|
406 |
lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText'])) |
|
407 |
lst.append(Preformatted(""" |
|
408 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
409 |
rowheights = (24, 16, 16, 16, 16) |
|
410 |
data = ( |
|
411 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', |
|
412 |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
413 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
414 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
415 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
416 |
('Hats', 893, 912, '1,212', 643, 789, 159, |
|
417 |
888, '1,298', 832, 453, '1,344','2,843') |
|
418 |
) |
|
419 |
t = Table(colwidths, rowheights, data) |
|
420 |
""", styleSheet['Code'], dedent=4)) |
|
421 |
lst.append(Paragraph(""" |
|
422 |
You can then give the Table a TableStyle object to control its format. The first TableStyle used was |
|
423 |
created as follows: |
|
424 |
""", styleSheet['BodyText'])) |
|
425 |
lst.append(Preformatted(""" |
|
6 | 426 |
GRID_STYLE = TableStyle( |
326 | 427 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
428 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
429 |
) |
|
430 |
""", styleSheet['Code'])) |
|
431 |
lst.append(Paragraph(""" |
|
432 |
TableStyles are created by passing in a list of commands. There are two types of commands - line commands |
|
433 |
and cell formatting commands. In all cases, the first three elements of a command are the command name, |
|
434 |
the starting cell and the ending cell. |
|
435 |
""", styleSheet['BodyText'])) |
|
436 |
lst.append(Paragraph(""" |
|
437 |
Line commands always follow this with the weight and color of the desired lines. Colors can be names, |
|
438 |
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 |
|
439 |
command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE |
|
440 |
and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and |
|
441 |
INNERGRID. |
|
442 |
""", styleSheet['BodyText'])) |
|
443 |
lst.append(Paragraph(""" |
|
444 |
Cell formatting commands are: |
|
445 |
""", styleSheet['BodyText'])) |
|
446 |
lst.append(Paragraph(""" |
|
447 |
FONT - takes fontname, fontsize and (optional) leading. |
|
448 |
""", styleSheet['Definition'])) |
|
449 |
lst.append(Paragraph(""" |
|
450 |
TEXTCOLOR - takes a color name or (R,G,B) tuple. |
|
451 |
""", styleSheet['Definition'])) |
|
452 |
lst.append(Paragraph(""" |
|
453 |
ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER). |
|
454 |
""", styleSheet['Definition'])) |
|
455 |
lst.append(Paragraph(""" |
|
456 |
LEFTPADDING - defaults to 6. |
|
457 |
""", styleSheet['Definition'])) |
|
458 |
lst.append(Paragraph(""" |
|
459 |
RIGHTPADDING - defaults to 6. |
|
460 |
""", styleSheet['Definition'])) |
|
461 |
lst.append(Paragraph(""" |
|
462 |
BOTTOMPADDING - defaults to 3. |
|
463 |
""", styleSheet['Definition'])) |
|
464 |
lst.append(Paragraph(""" |
|
465 |
A tablestyle is applied to a table by calling Table.setStyle(tablestyle). |
|
466 |
""", styleSheet['BodyText'])) |
|
467 |
t = Table(colwidths, rowheights, data) |
|
468 |
t.setStyle(GRID_STYLE) |
|
469 |
lst.append(PageBreak()) |
|
470 |
lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText'])) |
|
471 |
lst.append(t) |
|
472 |
||
473 |
t = Table(colwidths, rowheights, data) |
|
474 |
t.setStyle(BOX_STYLE) |
|
475 |
lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText'])) |
|
476 |
lst.append(t) |
|
477 |
lst.append(Paragraph(""" |
|
478 |
It was created as follows: |
|
479 |
""", styleSheet['BodyText'])) |
|
480 |
lst.append(Preformatted(""" |
|
6 | 481 |
BOX_STYLE = TableStyle( |
326 | 482 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
483 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
484 |
) |
|
485 |
""", styleSheet['Code'])) |
|
486 |
||
487 |
t = Table(colwidths, rowheights, data) |
|
488 |
t.setStyle(LABELED_GRID_STYLE) |
|
489 |
lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
490 |
lst.append(t) |
|
491 |
t = Table(colwidths, rowheights2, data2) |
|
492 |
t.setStyle(LABELED_GRID_STYLE) |
|
493 |
lst.append(Paragraph("This is LABELED_GRID_STYLE ILLUSTRATES EXPLICIT LINE SPLITTING WITH NEWLINE (different heights and data)\n", styleSheet['BodyText'])) |
|
494 |
lst.append(t) |
|
495 |
lst.append(Paragraph(""" |
|
496 |
It was created as follows: |
|
497 |
""", styleSheet['BodyText'])) |
|
498 |
lst.append(Preformatted(""" |
|
6 | 499 |
LABELED_GRID_STYLE = TableStyle( |
326 | 500 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
501 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
502 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
503 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
504 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
505 |
) |
|
506 |
""", styleSheet['Code'])) |
|
507 |
lst.append(PageBreak()) |
|
508 |
||
509 |
t = Table(colwidths, rowheights, data) |
|
510 |
t.setStyle(COLORED_GRID_STYLE) |
|
511 |
lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
512 |
lst.append(t) |
|
513 |
lst.append(Paragraph(""" |
|
514 |
It was created as follows: |
|
515 |
""", styleSheet['BodyText'])) |
|
516 |
lst.append(Preformatted(""" |
|
6 | 517 |
COLORED_GRID_STYLE = TableStyle( |
326 | 518 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
519 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
520 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
521 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
522 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
523 |
) |
|
524 |
""", styleSheet['Code'])) |
|
525 |
||
526 |
t = Table(colwidths, rowheights, data) |
|
527 |
t.setStyle(LIST_STYLE) |
|
528 |
lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText'])) |
|
529 |
lst.append(t) |
|
530 |
lst.append(Paragraph(""" |
|
531 |
It was created as follows: |
|
532 |
""", styleSheet['BodyText'])) |
|
533 |
lst.append(Preformatted(""" |
|
6 | 534 |
LIST_STYLE = TableStyle( |
326 | 535 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
536 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
537 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
538 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
539 |
) |
|
540 |
""", styleSheet['Code'])) |
|
6 | 541 |
|
326 | 542 |
t = Table(colwidths, rowheights, data) |
543 |
ts = TableStyle( |
|
544 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
|
545 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
546 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
547 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
548 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
549 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
550 |
) |
|
551 |
t.setStyle(ts) |
|
552 |
lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText'])) |
|
553 |
lst.append(t) |
|
554 |
lst.append(Paragraph(""" |
|
555 |
It was created as follows: |
|
556 |
""", styleSheet['BodyText'])) |
|
557 |
lst.append(Preformatted(""" |
|
6 | 558 |
ts = TableStyle( |
326 | 559 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
560 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
561 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
562 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
563 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
564 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
565 |
) |
|
566 |
""", styleSheet['Code'])) |
|
567 |
data = ( |
|
568 |
('', 'Jan\nCold', 'Feb\n', 'Mar\n','Apr\n','May\n', 'Jun\nHot', 'Jul\n', 'Aug\nThunder', 'Sep\n', 'Oct\n', 'Nov\n', 'Dec\n'), |
|
569 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
570 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
571 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
572 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
573 |
) |
|
574 |
c = list(colwidths) |
|
575 |
c[0] = None |
|
576 |
c[8] = None |
|
577 |
t = Table(c, [None]+list(rowheights[1:]), data) |
|
578 |
t.setStyle(LIST_STYLE) |
|
579 |
lst.append(Paragraph(""" |
|
580 |
This is a LIST_STYLE table with the first rowheight set to None ie automatic. |
|
581 |
The top row cells are split at a newline '\\n' character. The first and August |
|
582 |
column widths were also set to None. |
|
583 |
""", styleSheet['BodyText'])) |
|
584 |
lst.append(t) |
|
585 |
SimpleDocTemplate('testtables.pdf', showBoundary=1).build(lst) |
|
6 | 586 |
|
587 |
if __name__ == '__main__': |
|
326 | 588 |
test() |