reportlab/platypus/tables.py
author gmcm
Thu, 20 Jan 2000 23:33:04 +0000
changeset 0 f19e0a2433ab
child 6 eb791971b252
permissions -rwxr-xr-x
Initial revision
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     1
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     2
Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     3
row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     4
color and weight of the lines (if any), and the font, alignment and padding of the text.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     5
"""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     6
import layout
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     7
import operator
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     8
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
     9
_stringtype = type('')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    10
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    11
class CellStyle(layout.PropertySet):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    12
    defaults = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    13
        'fontname':'Times-Roman',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    14
        'fontsize':10,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    15
        'leading':12,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    16
        'leftPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    17
        'rightPadding':6,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    18
        'topPadding':3,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    19
        'bottomPadding':3,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    20
        'firstLineIndent':0,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    21
        'color':(0,0,0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    22
        'alignment': 'LEFT',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    23
        'background': (1,1,1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    24
        }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    25
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    26
class TableStyle:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    27
    def __init__(self, cmds=None):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    28
        self._cmds = cmds
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    29
        if cmds is None:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    30
            self._cmds = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    31
    def add(self, *cmd):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    32
        self._cmds.append(cmd)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    33
    def getCommands(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    34
        return self._cmds
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    35
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    36
class Table(layout.Drawable):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    37
    def __init__(self, colWidths, rowHeights, data):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    38
        if not colWidths:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    39
            raise ValueError, "Table must have at least 1 column"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    40
        if not rowHeights:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    41
            raise ValueError, "Table must have at least 1 row"
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    42
        nrows = self._nrows = len(rowHeights)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    43
        if len(data) != nrows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    44
            raise ValueError, "Data error - %d rows in data but %d in grid" % (len(data), nrows)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    45
        ncols = self._ncols = len(colWidths)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    46
        for i in range(nrows):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    47
            if len(data[i]) != ncols:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    48
                raise ValueError, "Not enough data points in row %d!" % i
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    49
        self._rowHeights = rowHeights
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    50
        self._colWidths = colWidths
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    51
        self._cellvalues = data
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    52
        dflt = CellStyle('<default>')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    53
        self._cellstyles = [None]*nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    54
        for i in range(nrows):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    55
            self._cellstyles[i] = [dflt]*ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    56
        self._bkgrndcmds = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    57
        self._linecmds = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    58
        height = self._height = reduce(operator.add, rowHeights, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    59
        self._rowpositions = [height]    # index 0 is actually topline; we skip when processing cells
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    60
        for h in rowHeights:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    61
            height = height - h
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    62
            self._rowpositions.append(height)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    63
        assert height == 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    64
        width = 0
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    65
        self._colpositions = [0]        #index -1 is right side boundary; we skip when processing cells
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    66
        for w in colWidths:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    67
            width = width + w
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    68
            self._colpositions.append(width)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    69
        self._width = width
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    70
        self._curweight = self._curcolor = self._curcellstyle = None
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    71
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    72
    def setStyle(self, tblstyle):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    73
        for cmd in tblstyle.getCommands():
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    74
            if cmd[0] == 'BACKGROUND':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    75
                self._bkgrndcmds.append(cmd)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    76
            elif _isLineCommand(cmd):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    77
                self._linecmds.append(cmd)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    78
            else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    79
                (op, (sc, sr), (ec, er)), values = cmd[:3] , cmd[3:]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    80
                if sc < 0: sc = sc + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    81
                if ec < 0: ec = ec + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    82
                if sr < 0: sr = sr + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    83
                if er < 0: er = er + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    84
                for i in range(sr, er+1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    85
                    for j in range(sc, ec+1):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    86
                        _setCellStyle(self._cellstyles, i, j, op, values)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    87
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    88
    def _drawLines(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    89
        for op, (sc, sr), (ec, er), weight, color in self._linecmds:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    90
            if sc < 0: sc = sc + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    91
            if ec < 0: ec = ec + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    92
            if sr < 0: sr = sr + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    93
            if er < 0: er = er + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    94
            if op == 'GRID':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    95
                self._drawBox( (sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    96
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    97
            elif op in ('BOX',  'OUTLINE',):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    98
                self._drawBox( (sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
    99
            elif op == 'INNERGRID':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   100
                self._drawInnerGrid( (sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   101
            elif op == 'LINEBELOW':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   102
                self._drawHLines((sc, sr+1), (ec, er+1), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   103
            elif op == 'LINEABOVE':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   104
                self._drawHLines((sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   105
            elif op == 'LINEBEFORE':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   106
                self._drawVLines((sc, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   107
            elif op == 'LINEAFTER':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   108
                self._drawVLines((sc+1, sr), (ec+1, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   109
            else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   110
                raise ValueError, "Unknown line style %s" % op
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   111
        self._curcolor = None
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   112
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   113
    def _drawBox(self,  (sc, sr), (ec, er), weight, color):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   114
        self._drawHLines((sc, sr), (ec, sr), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   115
        self._drawHLines((sc, er+1), (ec, er+1), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   116
        self._drawVLines((sc, sr), (sc, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   117
        self._drawVLines((ec+1, sr), (ec+1, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   118
    def _drawInnerGrid(self, (sc, sr), (ec, er), weight, color):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   119
        self._drawHLines((sc, sr+1), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   120
        self._drawVLines((sc+1, sr), (ec, er), weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   121
    def _prepLine(self, weight, color):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   122
        if type(color) is _stringtype:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   123
            color = COLORS.get(color, (0,0,0))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   124
        if color != self._curcolor:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   125
            apply(self.canv.setStrokeColorRGB, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   126
            self._curcolor = color
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   127
        if weight != self._curweight:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   128
            self.canv.setLineWidth(weight)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   129
            self._curweight = weight
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   130
    def _drawHLines(self, (sc, sr), (ec, er), weight, color):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   131
        self._prepLine(weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   132
        scp = self._colpositions[sc]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   133
        ecp = self._colpositions[ec+1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   134
        for rowpos in self._rowpositions[sr:er+1]:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   135
            self.canv.line(scp, rowpos, ecp, rowpos)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   136
    def _drawVLines(self, (sc, sr), (ec, er), weight, color):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   137
        self._prepLine(weight, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   138
        srp = self._rowpositions[sr]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   139
        erp = self._rowpositions[er+1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   140
        for colpos in self._colpositions[sc:ec+1]:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   141
            self.canv.line(colpos, srp, colpos, erp)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   142
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   143
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   144
    def wrap(self, availWidth, availHeight):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   145
        #nice and easy, since they are predetermined size
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   146
        self.availWidth = availWidth
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   147
        return (self._width, self._height)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   148
                
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   149
    def draw(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   150
        nudge = 0.5 * (self.availWidth - self._width)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   151
        self.canv.translate(nudge, 0)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   152
        self._drawBkgrnd()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   153
        self._drawLines()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   154
        for row, rowstyle, rowpos, rowheight in map(None, self._cellvalues, self._cellstyles, self._rowpositions[1:], self._rowHeights):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   155
            for cellval, cellstyle, colpos, colwidth in map(None, row, rowstyle, self._colpositions[:-1], self._colWidths):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   156
                self._drawCell(cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   157
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   158
    def _drawBkgrnd(self):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   159
        for cmd, (sc, sr), (ec, er), color in self._bkgrndcmds:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   160
            if sc < 0: sc = sc + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   161
            if ec < 0: ec = ec + self._ncols
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   162
            if sr < 0: sr = sr + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   163
            if er < 0: er = er + self._nrows
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   164
            if type(color) is _stringtype:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   165
                color = COLORS.get(values[0], (1,1,1))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   166
            x0 = self._colpositions[sc]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   167
            y0 = self._rowpositions[sr]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   168
            x1 = self._colpositions[ec+1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   169
            y1 = self._rowpositions[er+1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   170
            apply(self.canv.setFillColorRGB, color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   171
            self.canv.rect(x0, y0, x1-x0, y1-y0,stroke=0,fill=1)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   172
    def _drawCell(self, cellval, cellstyle, (colpos, rowpos), (colwidth, rowheight)):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   173
        #print "cellstyle is ", repr(cellstyle), id(cellstyle)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   174
        if self._curcellstyle is not cellstyle:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   175
            cur = self._curcellstyle
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   176
            if cur is None or cellstyle.color != cur.color:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   177
                #print "setting cell color to %s" % `cellstyle.color`
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   178
                apply(self.canv.setFillColorRGB, cellstyle.color)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   179
            if cur is None or cellstyle.leading != cur.leading or cellstyle.fontname != cur.fontname or cellstyle.fontsize != cur.fontsize:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   180
                #print "setting font: %s, %s, %s" % (cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   181
                self.canv.setFont(cellstyle.fontname, cellstyle.fontsize, cellstyle.leading)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   182
            self._curcellstyle = cellstyle
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   183
        just = cellstyle.alignment
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   184
        #print "alignment is ", just
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   185
        if just == 'LEFT':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   186
            draw = self.canv.drawString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   187
            x = colpos + cellstyle.leftPadding
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   188
        elif just in ('CENTRE', 'CENTER'):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   189
            draw = self.canv.drawCentredString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   190
            x = colpos + colwidth * 0.5
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   191
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   192
            draw = self.canv.drawRightString
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   193
            x = colpos + colwidth - cellstyle.rightPadding
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   194
        y = rowpos + cellstyle.bottomPadding
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   195
        if type(cellval) is _stringtype:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   196
            val = cellval
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   197
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   198
            val = str(cellval)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   199
        draw(x, y, val)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   200
        
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   201
# for text,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   202
#   drawCentredString(self, x, y, text) where x is center
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   203
#   drawRightString(self, x, y, text) where x is right
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   204
#   drawString(self, x, y, text) where x is left
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   205
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   206
LINECOMMANDS = (
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   207
    'GRID', 'BOX', 'OUTLINE', 'INNERGRID', 'LINEBELOW', 'LINEABOVE', 'LINEBEFORE', 'LINEAFTER', )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   208
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   209
COLORS = {
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   210
    'BLACK': (0,0,0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   211
    'RED': (1,0,0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   212
    'GREEN': (0,1,0),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   213
    'BLUE': (0,0,1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   214
    'WHITE':(1,1,1),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   215
    }
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   216
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   217
def _isLineCommand(cmd):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   218
    return cmd[0] in LINECOMMANDS
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   219
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   220
def _setCellStyle(cellstyles, i, j, op, values):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   221
    new = CellStyle('<%d, %d>' % (i,j), cellstyles[i][j])
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   222
    cellstyles[i][j] = new
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   223
    if op == 'FONT':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   224
        new.fontname = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   225
        new.fontsize = values[1]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   226
    elif op == 'TEXTCOLOR':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   227
        if type(values[0]) is _stringtype:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   228
            new.color = COLORS.get(values[0], (0,0,0))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   229
        else:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   230
            new.color = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   231
    elif op in ('ALIGN', 'ALIGNMENT'):
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   232
        new.alignment = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   233
    elif op == 'LEFTPADDING':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   234
        new.leftPadding = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   235
    elif op == 'RIGHTPADDING':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   236
        new.rightPadding = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   237
    elif op == 'TOPPADDING':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   238
        new.topPadding = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   239
    elif op == 'BOTTOMPADDING':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   240
        new.bottomPadding = values[0]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   241
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   242
GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   243
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   244
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   245
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   246
BOX_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   247
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   248
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   249
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   250
LABELED_GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   251
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   252
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   253
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   254
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   255
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   256
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   257
COLORED_GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   258
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   259
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   260
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   261
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   262
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   263
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   264
LIST_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   265
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   266
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   267
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   268
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   269
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   270
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   271
def test():
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   272
    rowheights = (24, 16, 16, 16, 16)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   273
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   274
    data = (
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   275
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   276
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   277
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   278
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   279
        ('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   280
        )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   281
    doc = layout.SimpleFlowDocument('testtables.pdf', platypus.DEFAULT_PAGE_SIZE, 1)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   282
    styleSheet = layout.getSampleStyleSheet()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   283
    lst = []
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   284
    lst.append(layout.Paragraph("Tables", styleSheet['Heading1']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   285
    lst.append(layout.Paragraph(__doc__, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   286
    lst.append(layout.Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   287
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   288
    colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   289
    rowheights = (24, 16, 16, 16, 16)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   290
    data = (
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   291
        ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   292
           'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   293
        ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   294
        ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   295
        ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   296
        ('Hats', 893, 912, '1,212', 643, 789, 159,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   297
             888, '1,298', 832, 453, '1,344','2,843')
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   298
        )   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   299
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   300
    """, styleSheet['Code'], dedent=4))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   301
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   302
    You can then give the Table a TableStyle object to control its format. The first TableStyle used was
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   303
    created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   304
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   305
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   306
GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   307
    [('GRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   308
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   309
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   310
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   311
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   312
    TableStyles are created by passing in a list of commands. There are two types of commands - line commands
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   313
    and cell formatting commands. In all cases, the first three elements of a command are the command name,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   314
    the starting cell and the ending cell.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   315
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   316
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   317
    Line commands always follow this with the weight and color of the desired lines. Colors can be names,
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   318
    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
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   319
    command names are: GRID, BOX, OUTLINE, INNERGRID, LINEBELOW, LINEABOVE, LINEBEFORE
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   320
    and LINEAFTER. BOX and OUTLINE are equivalent, and GRID is the equivalent of applying both BOX and
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   321
    INNERGRID.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   322
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   323
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   324
    Cell formatting commands are:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   325
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   326
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   327
    FONT - takes fontname, fontsize and (optional) leading.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   328
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   329
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   330
    TEXTCOLOR - takes a color name or (R,G,B) tuple.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   331
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   332
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   333
    ALIGNMENT (or ALIGN) - takes one of LEFT, RIGHT and CENTRE (or CENTER).
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   334
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   335
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   336
    LEFTPADDING - defaults to 6.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   337
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   338
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   339
    RIGHTPADDING - defaults to 6.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   340
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   341
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   342
    BOTTOMPADDING - defaults to 3.
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   343
    """, styleSheet['Definition']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   344
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   345
    A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   346
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   347
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   348
    t.setStyle(GRID_STYLE)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   349
    lst.append(layout.PageBreak())
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   350
    lst.append(layout.Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   351
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   352
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   353
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   354
    t.setStyle(BOX_STYLE)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   355
    lst.append(layout.Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   356
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   357
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   358
    It was created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   359
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   360
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   361
BOX_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   362
    [('BOX', (0,0), (-1,-1), 0.50, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   363
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   364
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   365
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   366
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   367
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   368
    t.setStyle(LABELED_GRID_STYLE)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   369
    lst.append(layout.Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   370
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   371
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   372
    It was created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   373
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   374
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   375
LABELED_GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   376
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   377
     ('BOX', (0,0), (-1,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   378
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   379
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   380
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   381
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   382
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   383
    lst.append(layout.PageBreak())
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   384
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   385
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   386
    t.setStyle(COLORED_GRID_STYLE)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   387
    lst.append(layout.Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   388
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   389
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   390
    It was created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   391
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   392
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   393
COLORED_GRID_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   394
    [('INNERGRID', (0,0), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   395
     ('BOX', (0,0), (-1,-1), 2, 'RED'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   396
     ('LINEBELOW', (0,0), (-1,0), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   397
     ('LINEAFTER', (0,0), (0,-1), 2, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   398
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   399
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   400
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   401
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   402
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   403
    t.setStyle(LIST_STYLE)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   404
    lst.append(layout.Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   405
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   406
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   407
    It was created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   408
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   409
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   410
LIST_STYLE = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   411
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   412
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   413
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   414
     ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   415
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   416
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   417
   
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   418
    t = Table(colwidths, rowheights,  data)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   419
    ts = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   420
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   421
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   422
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   423
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   424
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   425
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   426
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   427
    t.setStyle(ts)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   428
    lst.append(layout.Paragraph("This is a custom style\n", styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   429
    lst.append(t)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   430
    lst.append(layout.Paragraph("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   431
    It was created as follows:
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   432
    """, styleSheet['BodyText']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   433
    lst.append(layout.Preformatted("""
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   434
   ts = TableStyle(
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   435
    [('LINEABOVE', (0,0), (-1,0), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   436
     ('LINEABOVE', (0,1), (-1,-1), 0.25, 'BLACK'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   437
     ('LINEBELOW', (0,-1), (-1,-1), 2, 'GREEN'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   438
     ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   439
     ('TEXTCOLOR', (0,1), (0,-1), 'RED'),
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   440
     ('BACKGROUND', (0,0), (-1,0), (0,0.7,0.7))]
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   441
    )
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   442
    """, styleSheet['Code']))
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   443
    doc.build(lst)
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   444
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   445
if __name__ == '__main__':
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   446
    test()
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   447
    
f19e0a2433ab Initial revision
gmcm
parents:
diff changeset
   448