Fork me on GitHub

TableFactory by kstrauser

Easily create HTML, spreadsheet, or PDF tables from common Python data sources

TableFactory is a simple API for creating tables in popular formats. It acts as a wrapper around other popular Python report generators and handles all the tedious, boilerplate problems of extracting columns from input data, creating the layout, applying formatting to cells, etc.

Why?

I maintain an internal web application with many reports for our customers. After writing the code to generate a report's data, I'd make custom backends to write that data out in each of several formats that different customers might want. If someone asked me to add a new column to a report, I'd have to add it separately to each of the output types. That got old quickly.

TableFactory is my solution to the problem. It lets me spend my efforts on making efficient, useful reports instead of on the boring, repetitive process of formatting them.

Dependencies

HTML tables are made using only standard Python modules.

Spreadsheets are made with xlwt (http://pypi.python.org/pypi/xlwt).

PDFs are made with ReportLab (http://www.reportlab.com/software/opensource/).

Examples and Screenshots

All of the images below were taken from the built-in example/test function. It creates a list of dicts representing fake invoices, then uses them to create a table in each of the output formats.

rows = [...list of dicts, SQLAlchemy row objects, or just
        about anything else with columns that can be accessed
        by name or attribute...]

First, I made a "row specification" with three columns for the invoice number, a customer's name, and an invoice amount. The first argument to ColumnSpec is the name of the key or attribute to retrieve from a row object. The optional second argument is the name of the column in the generated report.

invoicerow = RowSpec(ColumnSpec('invoiceid', 'Invoice #'),
                     ColumnSpec('name', 'Customer Name'),
                     ColumnSpec('amount', 'Total', money=True))

Next, I used the invoicerow object to turn the list of invoice rows into a list of TableFactory's "TableRow" objects:

lines = invoicerow.makeall(rows)

Finally, I created each of the tables:

HTML

Although not apparent in the screenshot below, the HTMLTable generator uses CSS classes compatable with the tablesorter plugin for jQuery.

HTMLTable('Invoices by Customer',
          'Amount of each invoice, sorted by invoiceid',
          headers=invoicerow).render(lines)

Excel

SpreadsheetTable('Invoices by Customer',
                 'Amount of each invoice, sorted by invoiceid',
                 headers=invoicerow).render(lines)

PDF

PDFTable('Invoices by Customer',
         'Amount of each invoice, sorted by invoiceid',
         headers=invoicerow).render(lines)

Notice that the only difference in creating the different types of tables is in the class used to make each one. They share the same configuration and input data.

Install

The easy way: run easy_install TableFactory or pip install TableFactory to automatically download and install TableFactory with its dependencies.

The manual way: download TableFactory.py from https://github.com/kstrauser/tablefactory/blob/master/TableFactory.py and copy it to somewhere in your Python path. For example, /usr/local/lib/python2.6/site-packages might be a reasonable location on a Ubuntu system.

License

TableFactory is available under the permissive MIT License.

Authors

Kirk Strauser (kirk@strauser.com)

Contact

Kirk Strauser (kirk@strauser.com)

Download

You can download this project in either zip or tar formats.

You can also clone the project with Git by running:

$ git clone git://github.com/kstrauser/tablefactory