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