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