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