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