1 #copyright ReportLab Inc. 2000 |
1 #copyright ReportLab Inc. 2000 |
2 #see license.txt for license details |
2 #see license.txt for license details |
3 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/tables.py?cvsroot=reportlab |
3 #history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/tables.py?cvsroot=reportlab |
4 #$Header: /tmp/reportlab/reportlab/platypus/tables.py,v 1.53 2002/01/28 12:33:53 rgbecker Exp $ |
4 #$Header: /tmp/reportlab/reportlab/platypus/tables.py,v 1.54 2002/03/15 13:56:33 rgbecker Exp $ |
5 __version__=''' $Id: tables.py,v 1.53 2002/01/28 12:33:53 rgbecker Exp $ ''' |
5 __version__=''' $Id: tables.py,v 1.54 2002/03/15 13:56:33 rgbecker Exp $ ''' |
6 __doc__=""" |
6 __doc__=""" |
7 Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in |
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 |
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. |
9 color and weight of the lines (if any), and the font, alignment and padding of the text. |
10 |
10 |
18 tables and table styles. |
18 tables and table styles. |
19 """ |
19 """ |
20 |
20 |
21 from reportlab.platypus import * |
21 from reportlab.platypus import * |
22 from reportlab import rl_config |
22 from reportlab import rl_config |
23 from reportlab.lib.styles import PropertySet, getSampleStyleSheet |
23 from reportlab.lib.styles import PropertySet, getSampleStyleSheet, ParagraphStyle |
24 from reportlab.lib import colors |
24 from reportlab.lib import colors |
25 from reportlab.lib.utils import fp_str |
25 from reportlab.lib.utils import fp_str |
26 from reportlab.pdfbase import pdfmetrics |
26 from reportlab.pdfbase import pdfmetrics |
27 import operator, string |
27 import operator, string |
28 |
28 |
102 return type(x) not in _SeqTypes and 1 or len(x) |
102 return type(x) not in _SeqTypes and 1 or len(x) |
103 |
103 |
104 |
104 |
105 class Table(Flowable): |
105 class Table(Flowable): |
106 def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
106 def __init__(self, data, colWidths=None, rowHeights=None, style=None, |
107 repeatRows=0, repeatCols=0, splitByRow=1): |
107 repeatRows=0, repeatCols=0, splitByRow=1, emptyTableAction=None): |
108 #print "colWidths", colWidths |
108 #print "colWidths", colWidths |
109 self.hAlign = 'CENTER' |
109 self.hAlign = 'CENTER' |
110 self.vAlign = 'MIDDLE' |
110 self.vAlign = 'MIDDLE' |
|
111 if type(data) not in _SeqTypes: |
|
112 raise ValueError, "%s invalid data type" % self.identity() |
111 self._nrows = nrows = len(data) |
113 self._nrows = nrows = len(data) |
112 if len(data)==0 or type(data) not in _SeqTypes: |
114 if nrows: self._ncols = ncols = max(map(_rowLen,data)) |
113 raise ValueError, "%s must have at least 1 row" % self.identity() |
115 elif colWidths: ncols = len(colWidths) |
114 ncols = max(map(_rowLen,data)) |
116 else: ncols = 0 |
115 if not ncols: |
117 if not emptyTableAction: emptyTableAction = rl_config.emptyTableAction |
116 raise ValueError, "%s must have at least 1 column" % self.identity() |
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 |
117 if colWidths is None: colWidths = ncols*[None] |
136 if colWidths is None: colWidths = ncols*[None] |
118 elif len(colWidths) != ncols: |
137 elif len(colWidths) != ncols: |
119 raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths)) |
138 raise ValueError, "%s data error - %d columns in data but %d in grid" % (self.identity(),ncols, len(colWidths)) |
120 if rowHeights is None: rowHeights = nrows*[None] |
139 if rowHeights is None: rowHeights = nrows*[None] |
121 elif len(rowHeights) != nrows: |
140 elif len(rowHeights) != nrows: |
122 raise ValueError, "%s data error - %d rows in data but %d in grid" % (self.identity(),nrows, len(rowHeights)) |
141 raise ValueError, "%s data error - %d rows in data but %d in grid" % (self.identity(),nrows, len(rowHeights)) |
123 ncols = len(colWidths) |
|
124 for i in range(nrows): |
142 for i in range(nrows): |
125 if len(data[i]) != ncols: |
143 if len(data[i]) != ncols: |
126 raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i) |
144 raise ValueError, "%s not enough data points in row %d!" % (self.identity(),i) |
127 self._ncols = ncols |
|
128 self._rowHeights = self._argH = rowHeights |
145 self._rowHeights = self._argH = rowHeights |
129 self._colWidths = self._argW = colWidths |
146 self._colWidths = self._argW = colWidths |
130 self._cellvalues = data |
147 self._cellvalues = data |
131 ## dflt = CellStyle('<default>') |
148 ## dflt = CellStyle('<default>') |
132 ## |
149 ## |