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