author | rgbecker |
Wed, 01 Aug 2001 13:01:44 +0000 | |
changeset 1103 | 857af510186d |
parent 906 | 04370e5a8cfa |
child 1104 | 1c9c01e2a7b1 |
permissions | -rwxr-xr-x |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/tables.py?cvsroot=reportlab |
|
1103 | 4 |
#$Header: /tmp/reportlab/reportlab/platypus/tables.py,v 1.40 2001/08/01 13:01:44 rgbecker Exp $ |
5 |
__version__=''' $Id: tables.py,v 1.40 2001/08/01 13:01:44 rgbecker Exp $ ''' |
|
16 | 6 |
__doc__=""" |
6 | 7 |
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
8 |
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the |
|
9 |
color and weight of the lines (if any), and the font, alignment and padding of the text. |
|
268 | 10 |
|
327 | 11 |
None values in the sequence of row heights or column widths, mean that the corresponding rows |
12 |
or columns should be automatically sized. |
|
13 |
||
14 |
All the cell values should be convertible to strings; embedded newline '\\n' characters |
|
15 |
cause the value to wrap (ie are like a traditional linefeed). |
|
16 |
||
268 | 17 |
See the test output from running this module as a script for a discussion of the method for constructing |
18 |
tables and table styles. |
|
6 | 19 |
""" |
906 | 20 |
|
21 |
### HACK HACK HACK |
|
22 |
# NUDGE SHOULD BE DISABLED FOR TABLES INSIDE TABLES |
|
23 |
DO_NUDGE = 1 |
|
24 |
||
253 | 25 |
from reportlab.platypus import * |
129 | 26 |
from reportlab.lib.styles import PropertySet, getSampleStyleSheet |
168
02bac1346c69
Tables changed to use reportlab.lib.colors instead of
andy_robinson
parents:
129
diff
changeset
|
27 |
from reportlab.lib import colors |
1103 | 28 |
from reportlab.lib.utils import fp_str |
403 | 29 |
from reportlab.pdfbase import pdfmetrics |
312 | 30 |
import operator, string |
6 | 31 |
|
421 | 32 |
from types import TupleType, ListType, StringType |
6 | 33 |
|
128 | 34 |
class CellStyle(PropertySet): |
326 | 35 |
defaults = { |
36 |
'fontname':'Times-Roman', |
|
37 |
'fontsize':10, |
|
38 |
'leading':12, |
|
39 |
'leftPadding':6, |
|
40 |
'rightPadding':6, |
|
41 |
'topPadding':3, |
|
42 |
'bottomPadding':3, |
|
43 |
'firstLineIndent':0, |
|
44 |
'color':colors.black, |
|
45 |
'alignment': 'LEFT', |
|
46 |
'background': (1,1,1), |
|
329 | 47 |
'valign': 'BOTTOM', |
326 | 48 |
} |
6 | 49 |
|
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
50 |
# experimental replacement |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
51 |
class CellStyle1(PropertySet): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
52 |
fontname = "Times-Roman" |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
53 |
fontsize = 10 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
54 |
leading = 12 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
55 |
leftPadding = 6 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
56 |
rightPadding = 6 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
57 |
topPadding = 3 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
58 |
bottomPadding = 3 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
59 |
firstLineIndent = 0 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
60 |
color = colors.black |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
61 |
alignment = 'LEFT' |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
62 |
background = (1,1,1) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
63 |
valign = "BOTTOM" |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
64 |
def __init__(self, name, parent=None): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
65 |
self.name = name |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
66 |
if parent is not None: |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
67 |
parent.copy(self) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
68 |
def copy(self, result=None): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
69 |
if result is None: |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
70 |
result = CellStyle1() |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
71 |
for name in dir(self): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
72 |
setattr(result, name, gettattr(self, name)) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
73 |
return result |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
74 |
|
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
75 |
CellStyle = CellStyle1 |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
76 |
|
6 | 77 |
class TableStyle: |
326 | 78 |
def __init__(self, cmds=None): |
79 |
self._cmds = cmds |
|
80 |
if cmds is None: |
|
81 |
self._cmds = [] |
|
82 |
def add(self, *cmd): |
|
83 |
self._cmds.append(cmd) |
|
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
84 |
def __repr__(self): |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
85 |
L = map(repr, self._cmds) |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
86 |
import string |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
87 |
L = string.join(L, " \n") |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
88 |
return "TableStyle(\n%s\n) # end TableStyle" % L |
326 | 89 |
def getCommands(self): |
90 |
return self._cmds |
|
338 | 91 |
|
92 |
TableStyleType = type(TableStyle()) |
|
419 | 93 |
_SeqTypes = (TupleType, ListType) |
356 | 94 |
|
95 |
def _rowLen(x): |
|
419 | 96 |
return type(x) not in _SeqTypes and 1 or len(x) |
97 |
||
421 | 98 |
def _listCellGeom(V,w,s,W=None,H=None): |
419 | 99 |
aW = w-s.leftPadding-s.rightPadding |
100 |
t = 0 |
|
101 |
w = 0 |
|
102 |
for v in V: |
|
103 |
vw, vh = v.wrap(aW, 72000) |
|
104 |
if W is not None: W.append(vw) |
|
421 | 105 |
if H is not None: H.append(vh) |
419 | 106 |
w = max(w,vw) |
107 |
t = t + vh + v.getSpaceBefore()+v.getSpaceAfter() |
|
108 |
return w, t - V[0].getSpaceBefore()-V[-1].getSpaceAfter() |
|
356 | 109 |
|
221 | 110 |
class Table(Flowable): |
356 | 111 |
def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
350 | 112 |
repeatRows=0, repeatCols=0, splitByRow=1): |
904 | 113 |
#print "colWidths", colWidths |
1103 | 114 |
self._nrows = nrows = len(data) |
419 | 115 |
if len(data)==0 or type(data) not in _SeqTypes: |
1103 | 116 |
raise ValueError, "%s must have at least 1 row" % self.identity() |
356 | 117 |
ncols = max(map(_rowLen,data)) |
118 |
if not ncols: |
|
1103 | 119 |
raise ValueError, "%s must have at least 1 column" % self.identity() |
356 | 120 |
if colWidths is None: colWidths = ncols*[None] |
121 |
elif len(colWidths) != ncols: |
|
1103 | 122 |
raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths)) |
356 | 123 |
if rowHeights is None: rowHeights = nrows*[None] |
124 |
elif len(rowHeights) != nrows: |
|
1103 | 125 |
raise ValueError, "%s data error - %d rows in data but %d in grid" % (self.identity(),nrows, len(rowHeights)) |
126 |
ncols = len(colWidths) |
|
326 | 127 |
for i in range(nrows): |
128 |
if len(data[i]) != ncols: |
|
1103 | 129 |
raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i) |
130 |
self._ncols = ncols |
|
350 | 131 |
self._rowHeights = self._argH = rowHeights |
132 |
self._colWidths = self._argW = colWidths |
|
326 | 133 |
self._cellvalues = data |
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
134 |
## dflt = CellStyle('<default>') |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
135 |
## |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
136 |
## self._cellStyles = [None]*nrows |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
137 |
## for i in range(nrows): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
138 |
## self._cellStyles[i] = [dflt]*ncols |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
139 |
# make unique cell styles for each entry |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
140 |
cellrows = [] |
326 | 141 |
for i in range(nrows): |
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
142 |
cellcols = [] |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
143 |
for j in range(ncols): |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
144 |
cellcols.append(CellStyle(`(i,j)`)) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
145 |
cellrows.append(cellcols) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
146 |
self._cellStyles = cellrows |
350 | 147 |
|
326 | 148 |
self._bkgrndcmds = [] |
149 |
self._linecmds = [] |
|
150 |
self._curweight = self._curcolor = self._curcellstyle = None |
|
350 | 151 |
self.repeatRows = repeatRows |
152 |
self.repeatCols = repeatCols |
|
153 |
self.splitByRow = splitByRow |
|
6 | 154 |
|
342 | 155 |
if style: |
156 |
self.setStyle(style) |
|
541
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
157 |
def __repr__(self): |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
158 |
"incomplete, but better than nothing" |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
159 |
r = self._rowHeights |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
160 |
c = self._colWidths |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
161 |
cv = self._cellvalues |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
162 |
import pprint, string |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
163 |
cv = pprint.pformat(cv) |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
164 |
cv = string.replace(cv, "\n", "\n ") |
33de80b3655c
added __repr__s and enhanced exception messages for debugging
aaron_watters
parents:
537
diff
changeset
|
165 |
return "Table(\n rowHeights=%s,\n colWidths=%s,\n%s\n) # end table" % (r,c,cv) |
342 | 166 |
|
1103 | 167 |
def identity(self, maxLen): |
168 |
'''Identify our selves as well as possible''' |
|
169 |
vx = None |
|
170 |
nr = self._nrows |
|
171 |
if not hasattr(self,'_ncols'): |
|
172 |
nc = 'unknown' |
|
173 |
else: |
|
174 |
nc = self._ncols |
|
175 |
cv = self._cellvalues |
|
176 |
b = 0 |
|
177 |
for i in xrange(nr): |
|
178 |
for j in xrange(nc): |
|
179 |
v = cv[i][j] |
|
180 |
t = type(v) |
|
181 |
if t in _SeqTypes or isinstance(v,Flowable): |
|
182 |
if not t in _SeqTypes: v = (v,) |
|
183 |
r = '' |
|
184 |
for vij in v: |
|
185 |
r = vij.identity(maxLen) |
|
186 |
if r and r[-4:]!='>...': |
|
187 |
break |
|
188 |
if r and r[-4:]!='>...': |
|
189 |
ix, jx, vx, b = i, j, r, 1 |
|
190 |
else: |
|
191 |
v = v is None and '' or str(v) |
|
192 |
ix, jx, vx = i, j, v |
|
193 |
b = (vx and t is StringType) and 1 or 0 |
|
194 |
if maxLen: vx = vx[:maxLen] |
|
195 |
if b: break |
|
196 |
if b: break |
|
197 |
if vx: |
|
198 |
vx = ' with cell(%d,%d) containing\n%s' % (ix,jx,repr(vx)) |
|
199 |
else: |
|
200 |
vx = '...' |
|
201 |
||
202 |
return "<%s at %d %d rows x %s cols>%s" % (self.__class__.__name__, id(self), nr, nc, vx) |
|
203 |
||
326 | 204 |
def _calc(self): |
403 | 205 |
if hasattr(self,'_width'): return |
333 | 206 |
|
350 | 207 |
H = self._argH |
208 |
W = self._argW |
|
904 | 209 |
#print "W is", W |
6 | 210 |
|
326 | 211 |
if None in H: |
904 | 212 |
#print "none in H" |
326 | 213 |
H = H[:] #make a copy as we'll change it |
333 | 214 |
self._rowHeights = H |
326 | 215 |
while None in H: |
216 |
i = H.index(None) |
|
904 | 217 |
V = self._cellvalues[i] # values for row i |
218 |
S = self._cellStyles[i] # styles for row i |
|
326 | 219 |
h = 0 |
1103 | 220 |
j = 0 |
904 | 221 |
for v, s, w in map(None, V, S, W): # value, style, width (lengths must match) |
1103 | 222 |
j = j + 1 |
419 | 223 |
t = type(v) |
421 | 224 |
if t in _SeqTypes or isinstance(v,Flowable): |
225 |
if not t in _SeqTypes: v = (v,) |
|
419 | 226 |
if w is None: |
1103 | 227 |
raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width in\n%s" % (v[0].identity(30),i,j,self.identity(30)) |
496 | 228 |
dW,t = _listCellGeom(v,w,s) |
904 | 229 |
#print "leftpadding, rightpadding", s.leftPadding, s.rightPadding |
496 | 230 |
dW = dW + s.leftPadding + s.rightPadding |
231 |
if dW>w: |
|
1103 | 232 |
raise "LayoutError", "Flowable %s (%sx%s points) too wide for cell(%d,%d) (%sx* points) in\n%s" % (v[0].identity(30),fp_str(dW),fp_str(t),i,j, fp_str(w), self.identity(30)) |
419 | 233 |
else: |
421 | 234 |
if t is not StringType: |
235 |
v = v is None and '' or str(v) |
|
419 | 236 |
v = string.split(v, "\n") |
421 | 237 |
t = s.leading*len(v) |
238 |
t = t+s.bottomPadding+s.topPadding |
|
326 | 239 |
if t>h: h = t #record a new maximum |
240 |
H[i] = h |
|
6 | 241 |
|
326 | 242 |
if None in W: |
904 | 243 |
#print "none in w" |
326 | 244 |
W = W[:] |
245 |
self._colWidths = W |
|
246 |
while None in W: |
|
1103 | 247 |
j = W.index(None) |
248 |
f = lambda x,j=j: operator.getitem(x,j) |
|
326 | 249 |
V = map(f,self._cellvalues) |
350 | 250 |
S = map(f,self._cellStyles) |
326 | 251 |
w = 0 |
1103 | 252 |
i = 0 |
403 | 253 |
d = hasattr(self,'canv') and self.canv or pdfmetrics |
326 | 254 |
for v, s in map(None, V, S): |
1103 | 255 |
i = i + 1 |
419 | 256 |
t = type(v) |
421 | 257 |
if t in _SeqTypes or isinstance(v,Flowable): |
1103 | 258 |
raise ValueError, "Flowable %s in cell(%d,%d) can't have auto width\n%s" % (v.identity(30),i,j,self.identity(30)) |
421 | 259 |
elif t is not StringType: v = v is None and '' or str(v) |
326 | 260 |
v = string.split(v, "\n") |
261 |
t = s.leftPadding+s.rightPadding + max(map(lambda a, b=s.fontname, |
|
403 | 262 |
c=s.fontsize,d=d.stringWidth: d(a,b,c), v)) |
326 | 263 |
if t>w: w = t #record a new maximum |
1103 | 264 |
W[j] = w |
6 | 265 |
|
326 | 266 |
height = self._height = reduce(operator.add, H, 0) |
904 | 267 |
#print "height, H", height, H |
326 | 268 |
self._rowpositions = [height] # index 0 is actually topline; we skip when processing cells |
269 |
for h in H: |
|
270 |
height = height - h |
|
271 |
self._rowpositions.append(height) |
|
329 | 272 |
assert abs(height)<1e-8, 'Internal height error' |
326 | 273 |
width = 0 |
274 |
self._colpositions = [0] #index -1 is right side boundary; we skip when processing cells |
|
275 |
for w in W: |
|
904 | 276 |
#print w, width |
326 | 277 |
width = width + w |
278 |
self._colpositions.append(width) |
|
904 | 279 |
#print "final width", width |
280 |
||
326 | 281 |
self._width = width |
6 | 282 |
|
326 | 283 |
def setStyle(self, tblstyle): |
338 | 284 |
if type(tblstyle) is not TableStyleType: |
285 |
tblstyle = TableStyle(tblstyle) |
|
326 | 286 |
for cmd in tblstyle.getCommands(): |
350 | 287 |
self._addCommand(cmd) |
288 |
||
289 |
def _addCommand(self,cmd): |
|
290 |
if cmd[0] == 'BACKGROUND': |
|
291 |
self._bkgrndcmds.append(cmd) |
|
292 |
elif _isLineCommand(cmd): |
|
293 |
self._linecmds.append(cmd) |
|
294 |
else: |
|
295 |
(op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:] |
|
296 |
if sc < 0: sc = sc + self._ncols |
|
297 |
if ec < 0: ec = ec + self._ncols |
|
298 |
if sr < 0: sr = sr + self._nrows |
|
299 |
if er < 0: er = er + self._nrows |
|
300 |
for i in range(sr, er+1): |
|
301 |
for j in range(sc, ec+1): |
|
302 |
_setCellStyle(self._cellStyles, i, j, op, values) |
|
326 | 303 |
|
304 |
def _drawLines(self): |
|
904 | 305 |
# use round caps |
306 |
self.canv.setLineCap(1) |
|
326 | 307 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
308 |
if sc < 0: sc = sc + self._ncols |
|
309 |
if ec < 0: ec = ec + self._ncols |
|
310 |
if sr < 0: sr = sr + self._nrows |
|
311 |
if er < 0: er = er + self._nrows |
|
403 | 312 |
getattr(self,_LineOpMap.get(op, '_drawUnknown' ))( (sc, sr), (ec, er), weight, color) |
326 | 313 |
self._curcolor = None |
248 | 314 |
|
403 | 315 |
def _drawUnknown(self, (sc, sr), (ec, er), weight, color): |
316 |
raise ValueError, "Unknown line command '%s'" % op |
|
317 |
||
318 |
def _drawGrid(self, (sc, sr), (ec, er), weight, color): |
|
319 |
self._drawBox( (sc, sr), (ec, er), weight, color) |
|
320 |
self._drawInnerGrid( (sc, sr), (ec, er), weight, color) |
|
321 |
||
326 | 322 |
def _drawBox(self, (sc, sr), (ec, er), weight, color): |
323 |
self._drawHLines((sc, sr), (ec, sr), weight, color) |
|
324 |
self._drawHLines((sc, er+1), (ec, er+1), weight, color) |
|
325 |
self._drawVLines((sc, sr), (sc, er), weight, color) |
|
326 |
self._drawVLines((ec+1, sr), (ec+1, er), weight, color) |
|
350 | 327 |
|
326 | 328 |
def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color): |
329 |
self._drawHLines((sc, sr+1), (ec, er), weight, color) |
|
330 |
self._drawVLines((sc+1, sr), (ec, er), weight, color) |
|
350 | 331 |
|
326 | 332 |
def _prepLine(self, weight, color): |
333 |
if color != self._curcolor: |
|
334 |
self.canv.setStrokeColor(color) |
|
335 |
self._curcolor = color |
|
336 |
if weight != self._curweight: |
|
337 |
self.canv.setLineWidth(weight) |
|
338 |
self._curweight = weight |
|
350 | 339 |
|
326 | 340 |
def _drawHLines(self, (sc, sr), (ec, er), weight, color): |
341 |
self._prepLine(weight, color) |
|
342 |
scp = self._colpositions[sc] |
|
343 |
ecp = self._colpositions[ec+1] |
|
344 |
for rowpos in self._rowpositions[sr:er+1]: |
|
345 |
self.canv.line(scp, rowpos, ecp, rowpos) |
|
350 | 346 |
|
403 | 347 |
def _drawHLinesB(self, (sc, sr), (ec, er), weight, color): |
348 |
self._drawHLines((sc, sr+1), (ec, er+1), weight, color) |
|
349 |
||
326 | 350 |
def _drawVLines(self, (sc, sr), (ec, er), weight, color): |
351 |
self._prepLine(weight, color) |
|
352 |
srp = self._rowpositions[sr] |
|
353 |
erp = self._rowpositions[er+1] |
|
354 |
for colpos in self._colpositions[sc:ec+1]: |
|
355 |
self.canv.line(colpos, srp, colpos, erp) |
|
356 |
||
403 | 357 |
def _drawVLinesA(self, (sc, sr), (ec, er), weight, color): |
358 |
self._drawVLines((sc+1, sr), (ec+1, er), weight, color) |
|
359 |
||
326 | 360 |
def wrap(self, availWidth, availHeight): |
361 |
self._calc() |
|
362 |
#nice and easy, since they are predetermined size |
|
363 |
self.availWidth = availWidth |
|
904 | 364 |
#print "width, height", self._width, self._height |
326 | 365 |
return (self._width, self._height) |
904 | 366 |
#return (0, self._height) # debug |
419 | 367 |
def onSplit(self,T,byRow=1): |
368 |
''' |
|
369 |
This method will be called when the Table is split. |
|
370 |
Special purpose tables can override to do special stuff. |
|
371 |
''' |
|
372 |
pass |
|
373 |
||
350 | 374 |
def _cr_0(self,n,cmds): |
354 | 375 |
for c in cmds: |
376 |
c = tuple(c) |
|
377 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 378 |
if sr>=n: continue |
379 |
if er>=n: er = n-1 |
|
354 | 380 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 381 |
|
382 |
def _cr_1_1(self,n,repeatRows, cmds): |
|
354 | 383 |
for c in cmds: |
384 |
c = tuple(c) |
|
385 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 386 |
if sr>=0 and sr>=repeatRows and sr<n and er>=0 and er<n: continue |
387 |
if sr>=repeatRows and sr<n: sr=repeatRows |
|
388 |
elif sr>=repeatRows and sr>=n: sr=sr+repeatRows-n |
|
389 |
if er>=repeatRows and er<n: er=repeatRows |
|
390 |
elif er>=repeatRows and er>=n: er=er+repeatRows-n |
|
354 | 391 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 392 |
|
393 |
def _cr_1_0(self,n,cmds): |
|
354 | 394 |
for c in cmds: |
395 |
c = tuple(c) |
|
396 |
(sc,sr), (ec,er) = c[1:3] |
|
350 | 397 |
if er>=0 and er<n: continue |
398 |
if sr>=0 and sr<n: sr=0 |
|
399 |
if sr>=n: sr = sr-n |
|
400 |
if er>=n: er = er-n |
|
354 | 401 |
self._addCommand((c[0],)+((sc, sr), (ec, er))+c[3:]) |
350 | 402 |
|
403 |
def _splitRows(self,availHeight): |
|
404 |
h = 0 |
|
405 |
n = 0 |
|
406 |
lim = len(self._rowHeights) |
|
407 |
while n<lim: |
|
408 |
hn = h + self._rowHeights[n] |
|
409 |
if hn>availHeight: break |
|
410 |
h = hn |
|
411 |
n = n + 1 |
|
412 |
||
413 |
if n<=self.repeatRows: |
|
414 |
return [] |
|
415 |
||
416 |
if n==lim: return [self] |
|
417 |
||
418 |
repeatRows = self.repeatRows |
|
419 |
repeatCols = self.repeatCols |
|
420 |
splitByRow = self.splitByRow |
|
421 |
data = self._cellvalues |
|
422 |
||
423 |
#we're going to split into two superRows |
|
356 | 424 |
R0 = Table( data[:n], self._argW, self._argH[:n], |
350 | 425 |
repeatRows=repeatRows, repeatCols=repeatCols, |
426 |
splitByRow=splitByRow) |
|
427 |
||
428 |
#copy the styles and commands |
|
429 |
R0._cellStyles = self._cellStyles[:n] |
|
419 | 430 |
|
431 |
A = [] |
|
432 |
# hack up the line commands |
|
433 |
for op, (sc, sr), (ec, er), weight, color in self._linecmds: |
|
434 |
if sc < 0: sc = sc + self._ncols |
|
435 |
if ec < 0: ec = ec + self._ncols |
|
436 |
if sr < 0: sr = sr + self._nrows |
|
437 |
if er < 0: er = er + self._nrows |
|
438 |
||
439 |
if op in ('BOX','OUTLINE','GRID'): |
|
440 |
if sr<n and er>=n: |
|
441 |
# we have to split the BOX |
|
442 |
A.append(('LINEABOVE',(sc,sr), (ec,sr), weight, color)) |
|
443 |
A.append(('LINEBEFORE',(sc,sr), (sc,er), weight, color)) |
|
444 |
A.append(('LINEAFTER',(ec,sr), (ec,er), weight, color)) |
|
445 |
A.append(('LINEBELOW',(sc,er), (ec,er), weight, color)) |
|
446 |
if op=='GRID': |
|
447 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
421 | 448 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
419 | 449 |
A.append(('INNERGRID',(sc,sr), (ec,er), weight, color)) |
450 |
else: |
|
451 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
452 |
elif op in ('INNERGRID','LINEABOVE'): |
|
453 |
if sr<n and er>=n: |
|
454 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
421 | 455 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
419 | 456 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
457 |
elif op == 'LINEBELOW': |
|
421 | 458 |
if sr<n and er>=(n-1): |
419 | 459 |
A.append(('LINEABOVE',(sc,n), (ec,n), weight, color)) |
460 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
421 | 461 |
elif op == 'LINEABOVE': |
462 |
if sr<=n and er>=n: |
|
463 |
A.append(('LINEBELOW',(sc,n-1), (ec,n-1), weight, color)) |
|
464 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
419 | 465 |
else: |
466 |
A.append((op,(sc,sr), (ec,er), weight, color)) |
|
467 |
||
468 |
R0._cr_0(n,A) |
|
350 | 469 |
R0._cr_0(n,self._bkgrndcmds) |
470 |
||
471 |
if repeatRows: |
|
356 | 472 |
R1 = Table(data[:repeatRows]+data[n:], |
473 |
self._argW, self._argH[:repeatRows]+self._argH[n:], |
|
350 | 474 |
repeatRows=repeatRows, repeatCols=repeatCols, |
475 |
splitByRow=splitByRow) |
|
476 |
R1._cellStyles = self._cellStyles[:repeatRows]+self._cellStyles[n:] |
|
419 | 477 |
R1._cr_1_1(n,repeatRows,A) |
350 | 478 |
R1._cr_1_1(n,repeatRows,self._bkgrndcmds) |
479 |
else: |
|
356 | 480 |
R1 = Table(data[n:], self._argW, self._argH[n:], |
350 | 481 |
repeatRows=repeatRows, repeatCols=repeatCols, |
482 |
splitByRow=splitByRow) |
|
483 |
R1._cellStyles = self._cellStyles[n:] |
|
419 | 484 |
R1._cr_1_0(n,A) |
350 | 485 |
R1._cr_1_0(n,self._bkgrndcmds) |
486 |
||
419 | 487 |
self.onSplit(R0) |
488 |
self.onSplit(R1) |
|
350 | 489 |
return [R0,R1] |
490 |
||
491 |
def split(self, availWidth, availHeight): |
|
403 | 492 |
self._calc() |
350 | 493 |
if self.splitByRow: |
494 |
if self._width>availWidth: return [] |
|
495 |
return self._splitRows(availHeight) |
|
496 |
else: |
|
497 |
raise NotImplementedError |
|
403 | 498 |
|
326 | 499 |
def draw(self): |
906 | 500 |
global DO_NUDGE # ONLY NUDGE IF NOT IN ANOTHER TABLE (HACK) |
326 | 501 |
nudge = 0.5 * (self.availWidth - self._width) |
906 | 502 |
if DO_NUDGE: |
503 |
self.canv.translate(nudge, 0) |
|
504 |
OLD_DO_NUDGE = DO_NUDGE |
|
505 |
DO_NUDGE = 0 |
|
326 | 506 |
self._drawBkgrnd() |
507 |
self._drawLines() |
|
350 | 508 |
for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellStyles, self._rowpositions[1:], self._rowHeights): |
326 | 509 |
for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths): |
510 |
self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)) |
|
906 | 511 |
DO_NUDGE = OLD_DO_NUDGE |
326 | 512 |
|
513 |
def _drawBkgrnd(self): |
|
514 |
for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds: |
|
515 |
if sc < 0: sc = sc + self._ncols |
|
516 |
if ec < 0: ec = ec + self._ncols |
|
517 |
if sr < 0: sr = sr + self._nrows |
|
518 |
if er < 0: er = er + self._nrows |
|
419 | 519 |
color = colors.toColor(color) |
326 | 520 |
x0 = self._colpositions[sc] |
521 |
y0 = self._rowpositions[sr] |
|
522 |
x1 = self._colpositions[ec+1] |
|
523 |
y1 = self._rowpositions[er+1] |
|
524 |
self.canv.setFillColor(color) |
|
525 |
self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1) |
|
526 |
||
527 |
def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)): |
|
528 |
if self._curcellstyle is not cellstyle: |
|
529 |
cur = self._curcellstyle |
|
530 |
if cur is None or cellstyle.color != cur.color: |
|
531 |
self.canv.setFillColor(cellstyle.color) |
|
532 |
if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize: |
|
533 |
self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading) |
|
534 |
self._curcellstyle = cellstyle |
|
419 | 535 |
|
326 | 536 |
just = cellstyle.alignment |
419 | 537 |
valign = cellstyle.valign |
538 |
n = type(cellval) |
|
421 | 539 |
if n in _SeqTypes or isinstance(cellval,Flowable): |
540 |
if not n in _SeqTypes: cellval = (cellval,) |
|
419 | 541 |
# we assume it's a list of Flowables |
421 | 542 |
W = [] |
543 |
H = [] |
|
544 |
w, h = _listCellGeom(cellval,colwidth,cellstyle,W=W, H=H) |
|
419 | 545 |
if valign=='TOP': |
421 | 546 |
y = rowpos + rowheight - cellstyle.topPadding |
419 | 547 |
elif valign=='BOTTOM': |
421 | 548 |
y = rowpos+cellstyle.bottomPadding + h |
419 | 549 |
else: |
421 | 550 |
y = rowpos+(rowheight+cellstyle.bottomPadding-cellstyle.topPadding+h)/2.0 |
419 | 551 |
y = y+cellval[0].getSpaceBefore() |
421 | 552 |
for v, w, h in map(None,cellval,W,H): |
419 | 553 |
if just=='LEFT': x = colpos+cellstyle.leftPadding |
554 |
elif just=='RIGHT': x = colpos+colwidth-cellstyle.rightPadding - w |
|
421 | 555 |
elif just in ('CENTRE', 'CENTER'): |
556 |
x = colpos+(colwidth+cellstyle.leftPadding-cellstyle.rightPadding-w)/2.0 |
|
557 |
else: |
|
558 |
raise ValueError, 'Invalid justification %s' % just |
|
419 | 559 |
y = y - v.getSpaceBefore() |
421 | 560 |
y = y - h |
419 | 561 |
v.drawOn(self.canv,x,y) |
562 |
y = y - v.getSpaceAfter() |
|
326 | 563 |
else: |
419 | 564 |
if just == 'LEFT': |
565 |
draw = self.canv.drawString |
|
566 |
x = colpos + cellstyle.leftPadding |
|
567 |
elif just in ('CENTRE', 'CENTER'): |
|
568 |
draw = self.canv.drawCentredString |
|
569 |
x = colpos + colwidth * 0.5 |
|
570 |
elif just == 'RIGHT': |
|
571 |
draw = self.canv.drawRightString |
|
572 |
x = colpos + colwidth - cellstyle.rightPadding |
|
573 |
else: |
|
574 |
raise ValueError, 'Invalid justification %s' % just |
|
421 | 575 |
if n is StringType: val = cellval |
419 | 576 |
else: val = str(cellval) |
577 |
vals = string.split(val, "\n") |
|
578 |
n = len(vals) |
|
579 |
leading = cellstyle.leading |
|
580 |
fontsize = cellstyle.fontsize |
|
581 |
if valign=='BOTTOM': |
|
582 |
y = rowpos + cellstyle.bottomPadding+n*leading-fontsize |
|
583 |
elif valign=='TOP': |
|
584 |
y = rowpos + rowheight - cellstyle.topPadding - fontsize |
|
585 |
elif valign=='MIDDLE': |
|
586 |
y = rowpos + (cellstyle.bottomPadding + rowheight-cellstyle.topPadding+(n-1)*leading)/2.0 |
|
587 |
else: |
|
588 |
raise ValueError, "Bad valign: '%s'" % str(valign) |
|
329 | 589 |
|
419 | 590 |
for v in vals: |
591 |
draw(x, y, v) |
|
592 |
y = y-leading |
|
326 | 593 |
|
6 | 594 |
# for text, |
326 | 595 |
# drawCentredString(self, x, y, text) where x is center |
596 |
# drawRightString(self, x, y, text) where x is right |
|
597 |
# drawString(self, x, y, text) where x is left |
|
6 | 598 |
|
403 | 599 |
_LineOpMap = { 'GRID':'_drawGrid', |
600 |
'BOX':'_drawBox', |
|
601 |
'OUTLINE':'_drawBox', |
|
602 |
'INNERGRID':'_drawInnerGrid', |
|
603 |
'LINEBELOW':'_drawHLinesB', |
|
604 |
'LINEABOVE':'_drawHLines', |
|
605 |
'LINEBEFORE':'_drawVLines', |
|
606 |
'LINEAFTER':'_drawVLinesA', } |
|
6 | 607 |
|
403 | 608 |
LINECOMMANDS = _LineOpMap.keys() |
6 | 609 |
|
610 |
def _isLineCommand(cmd): |
|
326 | 611 |
return cmd[0] in LINECOMMANDS |
6 | 612 |
|
350 | 613 |
def _setCellStyle(cellStyles, i, j, op, values): |
730
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
614 |
#new = CellStyle('<%d, %d>' % (i,j), cellStyles[i][j]) |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
615 |
#cellStyles[i][j] = new |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
616 |
## modify in place!!! |
48a169b27fe4
changed handling of table element styles for better space/time. old code left commented
aaron_watters
parents:
684
diff
changeset
|
617 |
new = cellStyles[i][j] |
326 | 618 |
if op == 'FONT': |
357 | 619 |
n = len(values) |
326 | 620 |
new.fontname = values[0] |
357 | 621 |
if n>1: |
622 |
new.fontsize = values[1] |
|
361 | 623 |
if n>2: |
624 |
new.leading = values[2] |
|
625 |
else: |
|
626 |
new.leading = new.fontsize*1.2 |
|
357 | 627 |
elif op in ('FONTNAME', 'FACE'): |
628 |
new.fontname = values[0] |
|
629 |
elif op in ('SIZE', 'FONTSIZE'): |
|
630 |
new.fontsize = values[0] |
|
631 |
elif op == 'LEADING': |
|
632 |
new.leading = values[0] |
|
326 | 633 |
elif op == 'TEXTCOLOR': |
634 |
new.color = colors.toColor(values[0], colors.Color(0,0,0)) |
|
635 |
elif op in ('ALIGN', 'ALIGNMENT'): |
|
636 |
new.alignment = values[0] |
|
329 | 637 |
elif op == 'VALIGN': |
638 |
new.valign = values[0] |
|
326 | 639 |
elif op == 'LEFTPADDING': |
640 |
new.leftPadding = values[0] |
|
641 |
elif op == 'RIGHTPADDING': |
|
642 |
new.rightPadding = values[0] |
|
643 |
elif op == 'TOPPADDING': |
|
644 |
new.topPadding = values[0] |
|
645 |
elif op == 'BOTTOMPADDING': |
|
646 |
new.bottomPadding = values[0] |
|
6 | 647 |
|
648 |
GRID_STYLE = TableStyle( |
|
326 | 649 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
650 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
651 |
) |
|
6 | 652 |
BOX_STYLE = TableStyle( |
326 | 653 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
654 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
655 |
) |
|
6 | 656 |
LABELED_GRID_STYLE = TableStyle( |
326 | 657 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
658 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
659 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
660 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
661 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
662 |
) |
|
6 | 663 |
COLORED_GRID_STYLE = TableStyle( |
326 | 664 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
665 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
666 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
667 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
668 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
669 |
) |
|
6 | 670 |
LIST_STYLE = TableStyle( |
326 | 671 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
672 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
673 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
674 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
675 |
) |
|
6 | 676 |
|
677 |
def test(): |
|
329 | 678 |
from reportlab.lib.units import inch |
326 | 679 |
rowheights = (24, 16, 16, 16, 16) |
680 |
rowheights2 = (24, 16, 16, 16, 30) |
|
681 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
682 |
data = ( |
|
683 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
684 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
685 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
686 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
687 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
688 |
) |
|
689 |
data2 = ( |
|
690 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
691 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
692 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
693 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
694 |
('Hats\nLarge', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
695 |
) |
|
696 |
styleSheet = getSampleStyleSheet() |
|
697 |
lst = [] |
|
698 |
lst.append(Paragraph("Tables", styleSheet['Heading1'])) |
|
699 |
lst.append(Paragraph(__doc__, styleSheet['BodyText'])) |
|
700 |
lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText'])) |
|
701 |
lst.append(Preformatted(""" |
|
702 |
colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32) |
|
703 |
rowheights = (24, 16, 16, 16, 16) |
|
704 |
data = ( |
|
705 |
('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', |
|
706 |
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), |
|
707 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
708 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
709 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
710 |
('Hats', 893, 912, '1,212', 643, 789, 159, |
|
711 |
888, '1,298', 832, 453, '1,344','2,843') |
|
712 |
) |
|
356 | 713 |
t = Table(data, colwidths, rowheights) |
326 | 714 |
""", styleSheet['Code'], dedent=4)) |
715 |
lst.append(Paragraph(""" |
|
716 |
You can then give the Table a TableStyle object to control its format. The first TableStyle used was |
|
717 |
created as follows: |
|
718 |
""", styleSheet['BodyText'])) |
|
719 |
lst.append(Preformatted(""" |
|
6 | 720 |
GRID_STYLE = TableStyle( |
326 | 721 |
[('GRID', (0,0), (-1,-1), 0.25, colors.black), |
722 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
723 |
) |
|
724 |
""", styleSheet['Code'])) |
|
725 |
lst.append(Paragraph(""" |
|
726 |
TableStyles are created by passing in a list of commands. There are two types of commands - line commands |
|
727 |
and cell formatting commands. In all cases, the first three elements of a command are the command name, |
|
728 |
the starting cell and the ending cell. |
|
729 |
""", styleSheet['BodyText'])) |
|
730 |
lst.append(Paragraph(""" |
|
731 |
Line commands always follow this with the weight and color of the desired lines. Colors can be names, |
|
732 |
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 |
|
733 |
command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE |
|
734 |
and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and |
|
735 |
INNERGRID. |
|
736 |
""", styleSheet['BodyText'])) |
|
737 |
lst.append(Paragraph(""" |
|
738 |
Cell formatting commands are: |
|
739 |
""", styleSheet['BodyText'])) |
|
740 |
lst.append(Paragraph(""" |
|
741 |
FONT - takes fontname, fontsize and (optional) leading. |
|
742 |
""", styleSheet['Definition'])) |
|
743 |
lst.append(Paragraph(""" |
|
744 |
TEXTCOLOR - takes a color name or (R,G,B) tuple. |
|
745 |
""", styleSheet['Definition'])) |
|
746 |
lst.append(Paragraph(""" |
|
747 |
ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER). |
|
748 |
""", styleSheet['Definition'])) |
|
749 |
lst.append(Paragraph(""" |
|
750 |
LEFTPADDING - defaults to 6. |
|
751 |
""", styleSheet['Definition'])) |
|
752 |
lst.append(Paragraph(""" |
|
753 |
RIGHTPADDING - defaults to 6. |
|
754 |
""", styleSheet['Definition'])) |
|
755 |
lst.append(Paragraph(""" |
|
756 |
BOTTOMPADDING - defaults to 3. |
|
757 |
""", styleSheet['Definition'])) |
|
758 |
lst.append(Paragraph(""" |
|
759 |
A tablestyle is applied to a table by calling Table.setStyle(tablestyle). |
|
760 |
""", styleSheet['BodyText'])) |
|
356 | 761 |
t = Table(data, colwidths, rowheights) |
326 | 762 |
t.setStyle(GRID_STYLE) |
763 |
lst.append(PageBreak()) |
|
764 |
lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText'])) |
|
765 |
lst.append(t) |
|
766 |
||
356 | 767 |
t = Table(data, colwidths, rowheights) |
326 | 768 |
t.setStyle(BOX_STYLE) |
769 |
lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText'])) |
|
770 |
lst.append(t) |
|
771 |
lst.append(Paragraph(""" |
|
772 |
It was created as follows: |
|
773 |
""", styleSheet['BodyText'])) |
|
774 |
lst.append(Preformatted(""" |
|
6 | 775 |
BOX_STYLE = TableStyle( |
326 | 776 |
[('BOX', (0,0), (-1,-1), 0.50, colors.black), |
777 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
778 |
) |
|
779 |
""", styleSheet['Code'])) |
|
780 |
||
356 | 781 |
t = Table(data, colwidths, rowheights) |
326 | 782 |
t.setStyle(LABELED_GRID_STYLE) |
783 |
lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
784 |
lst.append(t) |
|
356 | 785 |
t = Table(data2, colwidths, rowheights2) |
326 | 786 |
t.setStyle(LABELED_GRID_STYLE) |
787 |
lst.append(Paragraph("This is LABELED_GRID_STYLE ILLUSTRATES EXPLICIT LINE SPLITTING WITH NEWLINE (different heights and data)\n", styleSheet['BodyText'])) |
|
788 |
lst.append(t) |
|
789 |
lst.append(Paragraph(""" |
|
790 |
It was created as follows: |
|
791 |
""", styleSheet['BodyText'])) |
|
792 |
lst.append(Preformatted(""" |
|
6 | 793 |
LABELED_GRID_STYLE = TableStyle( |
326 | 794 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
795 |
('BOX', (0,0), (-1,-1), 2, colors.black), |
|
796 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
797 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
798 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
799 |
) |
|
800 |
""", styleSheet['Code'])) |
|
801 |
lst.append(PageBreak()) |
|
802 |
||
356 | 803 |
t = Table(data, colwidths, rowheights) |
326 | 804 |
t.setStyle(COLORED_GRID_STYLE) |
805 |
lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText'])) |
|
806 |
lst.append(t) |
|
807 |
lst.append(Paragraph(""" |
|
808 |
It was created as follows: |
|
809 |
""", styleSheet['BodyText'])) |
|
810 |
lst.append(Preformatted(""" |
|
6 | 811 |
COLORED_GRID_STYLE = TableStyle( |
326 | 812 |
[('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
813 |
('BOX', (0,0), (-1,-1), 2, colors.red), |
|
814 |
('LINEBELOW', (0,0), (-1,0), 2, colors.black), |
|
815 |
('LINEAFTER', (0,0), (0,-1), 2, colors.black), |
|
816 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
817 |
) |
|
818 |
""", styleSheet['Code'])) |
|
819 |
||
356 | 820 |
t = Table(data, colwidths, rowheights) |
326 | 821 |
t.setStyle(LIST_STYLE) |
822 |
lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText'])) |
|
823 |
lst.append(t) |
|
824 |
lst.append(Paragraph(""" |
|
825 |
It was created as follows: |
|
826 |
""", styleSheet['BodyText'])) |
|
827 |
lst.append(Preformatted(""" |
|
6 | 828 |
LIST_STYLE = TableStyle( |
326 | 829 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
830 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
831 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
832 |
('ALIGN', (1,1), (-1,-1), 'RIGHT')] |
|
833 |
) |
|
834 |
""", styleSheet['Code'])) |
|
6 | 835 |
|
356 | 836 |
t = Table(data, colwidths, rowheights) |
326 | 837 |
ts = TableStyle( |
838 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
|
839 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
840 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
841 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
842 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
843 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
844 |
) |
|
845 |
t.setStyle(ts) |
|
846 |
lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText'])) |
|
847 |
lst.append(t) |
|
848 |
lst.append(Paragraph(""" |
|
849 |
It was created as follows: |
|
850 |
""", styleSheet['BodyText'])) |
|
851 |
lst.append(Preformatted(""" |
|
6 | 852 |
ts = TableStyle( |
326 | 853 |
[('LINEABOVE', (0,0), (-1,0), 2, colors.green), |
854 |
('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black), |
|
855 |
('LINEBELOW', (0,-1), (-1,-1), 2, colors.green), |
|
856 |
('ALIGN', (1,1), (-1,-1), 'RIGHT'), |
|
857 |
('TEXTCOLOR', (0,1), (0,-1), colors.red), |
|
858 |
('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))] |
|
859 |
) |
|
860 |
""", styleSheet['Code'])) |
|
861 |
data = ( |
|
862 |
('', 'Jan\nCold', 'Feb\n', 'Mar\n','Apr\n','May\n', 'Jun\nHot', 'Jul\n', 'Aug\nThunder', 'Sep\n', 'Oct\n', 'Nov\n', 'Dec\n'), |
|
863 |
('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89), |
|
864 |
('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119), |
|
865 |
('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13), |
|
866 |
('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843') |
|
867 |
) |
|
868 |
c = list(colwidths) |
|
869 |
c[0] = None |
|
870 |
c[8] = None |
|
356 | 871 |
t = Table(data, c, [None]+list(rowheights[1:])) |
326 | 872 |
t.setStyle(LIST_STYLE) |
873 |
lst.append(Paragraph(""" |
|
874 |
This is a LIST_STYLE table with the first rowheight set to None ie automatic. |
|
875 |
The top row cells are split at a newline '\\n' character. The first and August |
|
876 |
column widths were also set to None. |
|
877 |
""", styleSheet['BodyText'])) |
|
878 |
lst.append(t) |
|
329 | 879 |
lst.append(Paragraph(""" |
880 |
The red numbers should be aligned LEFT & BOTTOM, the blue RIGHT & TOP |
|
881 |
and the green CENTER & MIDDLE. |
|
882 |
""", styleSheet['BodyText'])) |
|
357 | 883 |
XY = [['X00y', 'X01y', 'X02y', 'X03y', 'X04y'], |
338 | 884 |
['X10y', 'X11y', 'X12y', 'X13y', 'X14y'], |
885 |
['X20y', 'X21y', 'X22y', 'X23y', 'X24y'], |
|
886 |
['X30y', 'X31y', 'X32y', 'X33y', 'X34y']] |
|
357 | 887 |
t=Table(XY, 5*[0.6*inch], 4*[0.6*inch]) |
338 | 888 |
t.setStyle([('ALIGN',(1,1),(-2,-2),'LEFT'), |
889 |
('TEXTCOLOR',(1,1),(-2,-2),colors.red), |
|
890 |
||
891 |
('VALIGN',(0,0),(1,-1),'TOP'), |
|
892 |
('ALIGN',(0,0),(1,-1),'RIGHT'), |
|
893 |
('TEXTCOLOR',(0,0),(1,-1),colors.blue), |
|
894 |
||
895 |
('ALIGN',(0,-1),(-1,-1),'CENTER'), |
|
896 |
('VALIGN',(0,-1),(-1,-1),'MIDDLE'), |
|
897 |
('TEXTCOLOR',(0,-1),(-1,-1),colors.green), |
|
898 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
899 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
900 |
]) |
|
329 | 901 |
lst.append(t) |
333 | 902 |
data = [('alignment', 'align\012alignment'), |
903 |
('bulletColor', 'bulletcolor\012bcolor'), |
|
904 |
('bulletFontName', 'bfont\012bulletfontname'), |
|
905 |
('bulletFontSize', 'bfontsize\012bulletfontsize'), |
|
906 |
('bulletIndent', 'bindent\012bulletindent'), |
|
907 |
('firstLineIndent', 'findent\012firstlineindent'), |
|
908 |
('fontName', 'face\012fontname\012font'), |
|
909 |
('fontSize', 'size\012fontsize'), |
|
910 |
('leading', 'leading'), |
|
911 |
('leftIndent', 'leftindent\012lindent'), |
|
912 |
('rightIndent', 'rightindent\012rindent'), |
|
913 |
('spaceAfter', 'spaceafter\012spacea'), |
|
914 |
('spaceBefore', 'spacebefore\012spaceb'), |
|
915 |
('textColor', 'fg\012textcolor\012color')] |
|
356 | 916 |
t = Table(data) |
338 | 917 |
t.setStyle([ |
333 | 918 |
('VALIGN',(0,0),(-1,-1),'TOP'), |
919 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
920 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
338 | 921 |
]) |
333 | 922 |
lst.append(t) |
403 | 923 |
t = Table([ ('Attribute', 'Synonyms'), |
924 |
('alignment', 'align, alignment'), |
|
925 |
('bulletColor', 'bulletcolor, bcolor'), |
|
926 |
('bulletFontName', 'bfont, bulletfontname'), |
|
927 |
('bulletFontSize', 'bfontsize, bulletfontsize'), |
|
928 |
('bulletIndent', 'bindent, bulletindent'), |
|
929 |
('firstLineIndent', 'findent, firstlineindent'), |
|
930 |
('fontName', 'face, fontname, font'), |
|
931 |
('fontSize', 'size, fontsize'), |
|
932 |
('leading', 'leading'), |
|
933 |
('leftIndent', 'leftindent, lindent'), |
|
934 |
('rightIndent', 'rightindent, rindent'), |
|
935 |
('spaceAfter', 'spaceafter, spacea'), |
|
936 |
('spaceBefore', 'spacebefore, spaceb'), |
|
937 |
('textColor', 'fg, textcolor, color')]) |
|
350 | 938 |
t.repeatRows = 1 |
939 |
t.setStyle([ |
|
940 |
('FONT',(0,0),(-1,1),'Times-Bold',10,12), |
|
941 |
('FONT',(0,1),(-1,-1),'Courier',8,8), |
|
942 |
('VALIGN',(0,0),(-1,-1),'MIDDLE'), |
|
943 |
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black), |
|
944 |
('BOX', (0,0), (-1,-1), 0.25, colors.black), |
|
354 | 945 |
('BACKGROUND', (0, 0), (-1, 0), colors.green), |
946 |
('BACKGROUND', (0, 1), (-1, -1), colors.pink), |
|
947 |
('ALIGN', (0, 0), (-1, 0), 'CENTER'), |
|
948 |
('ALIGN', (0, 1), (0, -1), 'LEFT'), |
|
949 |
('ALIGN', (-1, 1), (-1, -1), 'RIGHT'), |
|
950 |
('FONT', (0, 0), (-1, 0), 'Times-Bold', 12), |
|
951 |
('ALIGN', (1, 1), (1, -1), 'CENTER'), |
|
350 | 952 |
]) |
953 |
lst.append(t) |
|
357 | 954 |
lst.append(Table(XY, |
955 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 5,6), |
|
359 | 956 |
('GRID', (0,0), (-1,-1), 0.25, colors.blue),])) |
357 | 957 |
lst.append(Table(XY, |
958 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 10,12), |
|
359 | 959 |
('GRID', (0,0), (-1,-1), 0.25, colors.black),])) |
357 | 960 |
lst.append(Table(XY, |
961 |
style=[ ('FONT',(0,0),(-1,-1),'Times-Roman', 20,24), |
|
359 | 962 |
('GRID', (0,0), (-1,-1), 0.25, colors.red),])) |
403 | 963 |
lst.append(PageBreak()) |
964 |
data= [['00', '01', '02', '03', '04'], |
|
965 |
['10', '11', '12', '13', '14'], |
|
966 |
['20', '21', '22', '23', '24'], |
|
967 |
['30', '31', '32', '33', '34']] |
|
421 | 968 |
t=Table(data,style=[ |
969 |
('GRID',(0,0),(-1,-1),0.5,colors.grey), |
|
970 |
('GRID',(1,1),(-2,-2),1,colors.green), |
|
403 | 971 |
('BOX',(0,0),(1,-1),2,colors.red), |
421 | 972 |
('BOX',(0,0),(-1,-1),2,colors.black), |
403 | 973 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
974 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
419 | 975 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
976 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
977 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
403 | 978 |
]) |
979 |
lst.append(t) |
|
980 |
lst.append(Spacer(0,6)) |
|
981 |
for s in t.split(4*inch,30): |
|
982 |
lst.append(s) |
|
983 |
lst.append(Spacer(0,6)) |
|
984 |
lst.append(Spacer(0,6)) |
|
985 |
for s in t.split(4*inch,36): |
|
986 |
lst.append(s) |
|
987 |
lst.append(Spacer(0,6)) |
|
988 |
||
419 | 989 |
lst.append(Spacer(0,6)) |
990 |
for s in t.split(4*inch,56): |
|
991 |
lst.append(s) |
|
992 |
lst.append(Spacer(0,6)) |
|
993 |
||
421 | 994 |
import os, reportlab.platypus |
995 |
I = Image(os.path.join(os.path.dirname(reportlab.platypus.__file__),'..','demos','pythonpoint','leftlogo.gif')) |
|
996 |
I.drawHeight = 1.25*inch*I.drawHeight / I.drawWidth |
|
997 |
I.drawWidth = 1.25*inch |
|
1103 | 998 |
I.drawWidth = 9.25*inch #uncomment to see better messaging |
421 | 999 |
P = Paragraph("<para align=center spaceb=3>The <b>ReportLab Left <font color=red>Logo</font></b> Image</para>", styleSheet["BodyText"]) |
1000 |
data= [['A', 'B', 'C', Paragraph("<b>A pa<font color=red>r</font>a<i>graph</i></b><super><font color=yellow>1</font></super>",styleSheet["BodyText"]), 'D'], |
|
1001 |
['00', '01', '02', [I,P], '04'], |
|
1002 |
['10', '11', '12', [I,P], '14'], |
|
419 | 1003 |
['20', '21', '22', '23', '24'], |
1004 |
['30', '31', '32', '33', '34']] |
|
1005 |
||
1006 |
t=Table(data,style=[('GRID',(1,1),(-2,-2),1,colors.green), |
|
1007 |
('BOX',(0,0),(1,-1),2,colors.red), |
|
1008 |
('LINEABOVE',(1,2),(-2,2),1,colors.blue), |
|
1009 |
('LINEBEFORE',(2,1),(2,-2),1,colors.pink), |
|
1010 |
('BACKGROUND', (0, 0), (0, 1), colors.pink), |
|
1011 |
('BACKGROUND', (1, 1), (1, 2), colors.lavender), |
|
1012 |
('BACKGROUND', (2, 2), (2, 3), colors.orange), |
|
1013 |
('BOX',(0,0),(-1,-1),2,colors.black), |
|
1014 |
('GRID',(0,0),(-1,-1),0.5,colors.black), |
|
1015 |
('VALIGN',(3,0),(3,0),'BOTTOM'), |
|
1016 |
('BACKGROUND',(3,0),(3,0),colors.limegreen), |
|
421 | 1017 |
('BACKGROUND',(3,1),(3,1),colors.khaki), |
1018 |
('ALIGN',(3,1),(3,1),'CENTER'), |
|
1019 |
('BACKGROUND',(3,2),(3,2),colors.beige), |
|
1020 |
('ALIGN',(3,2),(3,2),'LEFT'), |
|
419 | 1021 |
]) |
1022 |
||
1023 |
t._argW[3]=1.5*inch |
|
1024 |
lst.append(t) |
|
1025 |
||
356 | 1026 |
SimpleDocTemplate('tables.pdf', showBoundary=1).build(lst) |
6 | 1027 |
|
1028 |
if __name__ == '__main__': |
|
326 | 1029 |
test() |