reportlab/platypus/tables.py
changeset 221 3d71b66b14c6
parent 168 02bac1346c69
child 248 c103b7a55e79
equal deleted inserted replaced
220:3b27693ac273 221:3d71b66b14c6
    29 # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    29 # OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
    30 # PERFORMANCE OF THIS SOFTWARE. 
    30 # PERFORMANCE OF THIS SOFTWARE. 
    31 #
    31 #
    32 ###############################################################################
    32 ###############################################################################
    33 #	$Log: tables.py,v $
    33 #	$Log: tables.py,v $
       
    34 #	Revision 1.9  2000/05/16 16:15:16  rgbecker
       
    35 #	Changes related to removal of SimpleFlowDocument
       
    36 #
    34 #	Revision 1.8  2000/04/26 11:07:15  andy_robinson
    37 #	Revision 1.8  2000/04/26 11:07:15  andy_robinson
    35 #	Tables changed to use reportlab.lib.colors instead of
    38 #	Tables changed to use reportlab.lib.colors instead of
    36 #	the six hard-coded color strings there previously.
    39 #	the six hard-coded color strings there previously.
    37 #
    40 #	
    38 #	Revision 1.7  2000/04/14 12:17:05  rgbecker
    41 #	Revision 1.7  2000/04/14 12:17:05  rgbecker
    39 #	Splitting layout.py
    42 #	Splitting layout.py
    40 #	
    43 #	
    41 #	Revision 1.6  2000/04/14 11:54:57  rgbecker
    44 #	Revision 1.6  2000/04/14 11:54:57  rgbecker
    42 #	Splitting layout.py
    45 #	Splitting layout.py
    51 #	License text fixes
    54 #	License text fixes
    52 #	
    55 #	
    53 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
    56 #	Revision 1.2  2000/02/15 15:47:09  rgbecker
    54 #	Added license, __version__ and Logi comment
    57 #	Added license, __version__ and Logi comment
    55 #	
    58 #	
    56 __version__=''' $Id: tables.py,v 1.8 2000/04/26 11:07:15 andy_robinson Exp $ '''
    59 __version__=''' $Id: tables.py,v 1.9 2000/05/16 16:15:16 rgbecker Exp $ '''
    57 __doc__="""
    60 __doc__="""
    58 Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in
    61 Tables are created by passing the constructor a tuple of column widths, a tuple of row heights and the data in
    59 row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the
    62 row order. Drawing of the table can be controlled by using a TableStyle instance. This allows control of the
    60 color and weight of the lines (if any), and the font, alignment and padding of the text.
    63 color and weight of the lines (if any), and the font, alignment and padding of the text.
    61 """
    64 """
    62 from reportlab.platypus import layout
    65 from reportlab.platypus.doctemplate import *
    63 from reportlab.platypus.paragraph import Paragraph
    66 from reportlab.platypus.paragraph import Paragraph
    64 from reportlab.lib.styles import PropertySet, getSampleStyleSheet
    67 from reportlab.lib.styles import PropertySet, getSampleStyleSheet
    65 from reportlab.lib import colors
    68 from reportlab.lib import colors
    66 import operator
    69 import operator
    67 
    70 
    90     def add(self, *cmd):
    93     def add(self, *cmd):
    91         self._cmds.append(cmd)
    94         self._cmds.append(cmd)
    92     def getCommands(self):
    95     def getCommands(self):
    93         return self._cmds
    96         return self._cmds
    94         
    97         
    95 class Table(layout.Flowable):
    98 class Table(Flowable):
    96     def __init__(self, colWidths, rowHeights, data):
    99     def __init__(self, colWidths, rowHeights, data):
    97         if not colWidths:
   100         if not colWidths:
    98             raise ValueError, "Table must have at least 1 column"
   101             raise ValueError, "Table must have at least 1 column"
    99         if not rowHeights:
   102         if not rowHeights:
   100             raise ValueError, "Table must have at least 1 row"
   103             raise ValueError, "Table must have at least 1 row"
   326         ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
   329         ('Mugs', 0, 4, 17, 3, 21, 47, 12, 33, 2, -2, 44, 89),
   327         ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
   330         ('T-Shirts', 0, 42, 9, -3, 16, 4, 72, 89, 3, 19, 32, 119),
   328         ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
   331         ('Key Ring', 0,0,0,0,0,0,1,0,0,0,2,13),
   329         ('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
   332         ('Hats', 893, 912, '1,212', 643, 789, 159, 888, '1,298', 832, 453, '1,344','2,843')
   330         )
   333         )
   331     doc = layout.SimpleFlowDocument('testtables.pdf', layout.DEFAULT_PAGE_SIZE, 1)
       
   332     styleSheet = getSampleStyleSheet()
   334     styleSheet = getSampleStyleSheet()
   333     lst = []
   335     lst = []
   334     lst.append(Paragraph("Tables", styleSheet['Heading1']))
   336     lst.append(Paragraph("Tables", styleSheet['Heading1']))
   335     lst.append(Paragraph(__doc__, styleSheet['BodyText']))
   337     lst.append(Paragraph(__doc__, styleSheet['BodyText']))
   336     lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
   338     lst.append(Paragraph("The Tables (shown in different styles below) were created using the following code:", styleSheet['BodyText']))
   337     lst.append(layout.Preformatted("""
   339     lst.append(Preformatted("""
   338     colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
   340     colwidths = (50, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32)
   339     rowheights = (24, 16, 16, 16, 16)
   341     rowheights = (24, 16, 16, 16, 16)
   340     data = (
   342     data = (
   341         ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
   343         ('', 'Jan', 'Feb', 'Mar','Apr','May', 'Jun',
   342            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
   344            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
   350     """, styleSheet['Code'], dedent=4))
   352     """, styleSheet['Code'], dedent=4))
   351     lst.append(Paragraph("""
   353     lst.append(Paragraph("""
   352     You can then give the Table a TableStyle object to control its format. The first TableStyle used was
   354     You can then give the Table a TableStyle object to control its format. The first TableStyle used was
   353     created as follows:
   355     created as follows:
   354     """, styleSheet['BodyText']))
   356     """, styleSheet['BodyText']))
   355     lst.append(layout.Preformatted("""
   357     lst.append(Preformatted("""
   356 GRID_STYLE = TableStyle(
   358 GRID_STYLE = TableStyle(
   357     [('GRID', (0,0), (-1,-1), 0.25, colors.black),
   359     [('GRID', (0,0), (-1,-1), 0.25, colors.black),
   358      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   360      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   359     )
   361     )
   360     """, styleSheet['Code']))
   362     """, styleSheet['Code']))
   394     lst.append(Paragraph("""
   396     lst.append(Paragraph("""
   395     A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
   397     A tablestyle is applied to a table by calling Table.setStyle(tablestyle).
   396     """, styleSheet['BodyText']))
   398     """, styleSheet['BodyText']))
   397     t = Table(colwidths, rowheights,  data)
   399     t = Table(colwidths, rowheights,  data)
   398     t.setStyle(GRID_STYLE)
   400     t.setStyle(GRID_STYLE)
   399     lst.append(layout.PageBreak())
   401     lst.append(PageBreak())
   400     lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
   402     lst.append(Paragraph("This is GRID_STYLE\n", styleSheet['BodyText']))
   401     lst.append(t)
   403     lst.append(t)
   402     
   404     
   403     t = Table(colwidths, rowheights,  data)
   405     t = Table(colwidths, rowheights,  data)
   404     t.setStyle(BOX_STYLE)
   406     t.setStyle(BOX_STYLE)
   405     lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
   407     lst.append(Paragraph("This is BOX_STYLE\n", styleSheet['BodyText']))
   406     lst.append(t)
   408     lst.append(t)
   407     lst.append(Paragraph("""
   409     lst.append(Paragraph("""
   408     It was created as follows:
   410     It was created as follows:
   409     """, styleSheet['BodyText']))
   411     """, styleSheet['BodyText']))
   410     lst.append(layout.Preformatted("""
   412     lst.append(Preformatted("""
   411 BOX_STYLE = TableStyle(
   413 BOX_STYLE = TableStyle(
   412     [('BOX', (0,0), (-1,-1), 0.50, colors.black),
   414     [('BOX', (0,0), (-1,-1), 0.50, colors.black),
   413      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   415      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   414     )
   416     )
   415     """, styleSheet['Code']))
   417     """, styleSheet['Code']))
   419     lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
   421     lst.append(Paragraph("This is LABELED_GRID_STYLE\n", styleSheet['BodyText']))
   420     lst.append(t)
   422     lst.append(t)
   421     lst.append(Paragraph("""
   423     lst.append(Paragraph("""
   422     It was created as follows:
   424     It was created as follows:
   423     """, styleSheet['BodyText']))
   425     """, styleSheet['BodyText']))
   424     lst.append(layout.Preformatted("""
   426     lst.append(Preformatted("""
   425 LABELED_GRID_STYLE = TableStyle(
   427 LABELED_GRID_STYLE = TableStyle(
   426     [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
   428     [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
   427      ('BOX', (0,0), (-1,-1), 2, colors.black),
   429      ('BOX', (0,0), (-1,-1), 2, colors.black),
   428      ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
   430      ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
   429      ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
   431      ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
   430      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   432      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   431     )
   433     )
   432     """, styleSheet['Code']))
   434     """, styleSheet['Code']))
   433     lst.append(layout.PageBreak())
   435     lst.append(PageBreak())
   434     
   436     
   435     t = Table(colwidths, rowheights,  data)
   437     t = Table(colwidths, rowheights,  data)
   436     t.setStyle(COLORED_GRID_STYLE)
   438     t.setStyle(COLORED_GRID_STYLE)
   437     lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
   439     lst.append(Paragraph("This is COLORED_GRID_STYLE\n", styleSheet['BodyText']))
   438     lst.append(t)
   440     lst.append(t)
   439     lst.append(Paragraph("""
   441     lst.append(Paragraph("""
   440     It was created as follows:
   442     It was created as follows:
   441     """, styleSheet['BodyText']))
   443     """, styleSheet['BodyText']))
   442     lst.append(layout.Preformatted("""
   444     lst.append(Preformatted("""
   443 COLORED_GRID_STYLE = TableStyle(
   445 COLORED_GRID_STYLE = TableStyle(
   444     [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
   446     [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
   445      ('BOX', (0,0), (-1,-1), 2, colors.red),
   447      ('BOX', (0,0), (-1,-1), 2, colors.red),
   446      ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
   448      ('LINEBELOW', (0,0), (-1,0), 2, colors.black),
   447      ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
   449      ('LINEAFTER', (0,0), (0,-1), 2, colors.black),
   454     lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
   456     lst.append(Paragraph("This is LIST_STYLE\n", styleSheet['BodyText']))
   455     lst.append(t)
   457     lst.append(t)
   456     lst.append(Paragraph("""
   458     lst.append(Paragraph("""
   457     It was created as follows:
   459     It was created as follows:
   458     """, styleSheet['BodyText']))
   460     """, styleSheet['BodyText']))
   459     lst.append(layout.Preformatted("""
   461     lst.append(Preformatted("""
   460 LIST_STYLE = TableStyle(
   462 LIST_STYLE = TableStyle(
   461     [('LINEABOVE', (0,0), (-1,0), 2, colors.green),
   463     [('LINEABOVE', (0,0), (-1,0), 2, colors.green),
   462      ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
   464      ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
   463      ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
   465      ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
   464      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   466      ('ALIGN', (1,1), (-1,-1), 'RIGHT')]
   478     lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText']))
   480     lst.append(Paragraph("This is a custom style\n", styleSheet['BodyText']))
   479     lst.append(t)
   481     lst.append(t)
   480     lst.append(Paragraph("""
   482     lst.append(Paragraph("""
   481     It was created as follows:
   483     It was created as follows:
   482     """, styleSheet['BodyText']))
   484     """, styleSheet['BodyText']))
   483     lst.append(layout.Preformatted("""
   485     lst.append(Preformatted("""
   484    ts = TableStyle(
   486    ts = TableStyle(
   485     [('LINEABOVE', (0,0), (-1,0), 2, colors.green),
   487     [('LINEABOVE', (0,0), (-1,0), 2, colors.green),
   486      ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
   488      ('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
   487      ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
   489      ('LINEBELOW', (0,-1), (-1,-1), 2, colors.green),
   488      ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
   490      ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
   489      ('TEXTCOLOR', (0,1), (0,-1), colors.red),
   491      ('TEXTCOLOR', (0,1), (0,-1), colors.red),
   490      ('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))]
   492      ('BACKGROUND', (0,0), (-1,0), colors.Color(0,0.7,0.7))]
   491     )
   493     )
   492     """, styleSheet['Code']))
   494     """, styleSheet['Code']))
   493     doc.build(lst)
   495     SimpleDocTemplate('testtables.pdf', DEFAULT_PAGE_SIZE, showBoundary=1).build(lst)
   494 
   496 
   495 if __name__ == '__main__':
   497 if __name__ == '__main__':
   496     test()
   498     test()