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