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