[reportlab-users] Problems understanding TableStyle

Anders Schneiderman reportlab-users@reportlab.com
Tue, 07 Oct 2003 08:42:31 -0400


I just started using tables, and I could really use some help. 
According to the manual, when you use TableStyle, you hand it the
command, a string, and then "the remaining elements of the command tuple
represent the start and stop cell coordinates of the command."  When
I've looked at examples, I haven't been able to figure out how the start
and stop cell coordinates work.  

For ex, the reportlab test script testtables.py includes the following
to create a table:

-----------------------------
def getTable():
    t = Table((('','North','South','East','West'),
             ('Quarter 1',100,200,300,400),
             ('Quarter 2',100,400,600,800),
             ('Total',300,600,900,'1,200')),
            )
    return t
-----------------------------

It then creates a list of styles and cumulatively applies them to the
table:
-----------------------------
def makeStyles():
    styles = []
    for i in range(5):
        styles.append(TableStyle([('ALIGN', (1,1), (-1,-1), 'RIGHT'),
                                         ('ALIGN', (0,0), (-1,0),
'CENTRE') ]))
    for style in styles[1:]:
        style.add('GRID', (0,0), (-1,-1), 0.25, colors.black)
    for style in styles[2:]:
        style.add('LINEBELOW', (0,0), (-1,0), 2, colors.black)
    for style in styles[3:]:
        style.add('LINEABOVE', (0, -1), (-1,-1), 2, colors.black)
    styles[-1].add('LINEBELOW',(1,-1), (-1, -1), 2, (0.5, 0.5, 0.5))
    return styles
-----------------------------

When you look at the output, you can see that 
         style.add('GRID', (0,0), (-1,-1), 0.25, colors.black)
produces a grid around the whole 4 X 5 table

        style.add('LINEBELOW', (0,0), (-1,0), 2, colors.black)
produces a line below the first row

        style.add('LINEABOVE', (0, -1), (-1,-1), 2, colors.black)
produces a line above the last row 

    styles[-1].add('LINEBELOW',(1,-1), (-1, -1), 2, (0.5, 0.5, 0.5))
produces a line below the last row on cells 300, 500, 900, 1200

Clearly this aren't your standard cartesian coordinates, and I couldn't
find anything in the user manual or in the archives of this mailing list
that explains how it works.  What system is it using??

Thanks,
Anders Schneiderman





P.S.  Here's the script in its entirety:

#!/bin/env python
#copyright ReportLab Inc. 2000
#see license.txt for license details
#history
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/platypus/test/testtables.py?cvsroot=reportlab
#$Header: /cvsroot/reportlab/reportlab/test/test_platypus_tables.py,v
1.3 2002/07/24 19:56:38 andy_robinson Exp $
__version__=''' $Id: test_platypus_tables.py,v 1.3 2002/07/24 19:56:38
andy_robinson Exp $ '''
__doc__='Test script for reportlab.tables'

from reportlab.test import unittest
from reportlab.test.utils import makeSuiteForClasses

from reportlab.platypus import Spacer, SimpleDocTemplate, Table,
TableStyle
from reportlab.lib.units import inch
from reportlab.lib import colors


def getTable():
    t = Table((('','North','South','East','West'),
             ('Quarter 1',100,200,300,400),
             ('Quarter 2',100,400,600,800),
             ('Total',300,600,900,'1,200')),
             (72,36,36,36,36),
             (24, 16,16,18)
            )
    return t


def makeStyles():
    styles = []
    for i in range(5):
        styles.append(TableStyle([('ALIGN', (1,1), (-1,-1), 'RIGHT'),
                                         ('ALIGN', (0,0), (-1,0),
'CENTRE') ]))
    for style in styles[1:]:
        style.add('GRID', (0,0), (-1,-1), 0.25, colors.black)
    for style in styles[2:]:
        style.add('LINEBELOW', (0,0), (-1,0), 2, colors.black)
    for style in styles[3:]:
        style.add('LINEABOVE', (0, -1), (-1,-1), 2, colors.black)
    styles[-1].add('LINEBELOW',(1,-1), (-1, -1), 2, (0.5, 0.5, 0.5))
    return styles


def run():
    doc = SimpleDocTemplate('test_platypus_tables.pdf',
pagesize=(8.5*inch, 11*inch), showBoundary=1)
    styles = makeStyles()
    lst = []
    for style in styles:
        t = getTable()
        t.setStyle(style)
##        print '--------------'
##        for rowstyle in t._cellstyles:
##            for s in rowstyle:
##                print s.alignment
        lst.append(t)
        lst.append(Spacer(0,12))
    doc.build(lst)


class TablesTestCase(unittest.TestCase):
    "Make documents with tables"

    def test0(self):
        "Make a document full of tables"
        run()


def makeSuite():
    return makeSuiteForClasses(TablesTestCase)


#noruntests
if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())


#LINEABOVE
#LINEBELOW
#LINEBEFORE
#LINEAFTER
#GRID
#BOX
#INNERGRID ??

#FONT
#TEXTCOLOR
#ALIGNMENT
#PADDING