PyTablePrinter

I recently started working on some tables and supplementary materials for an academic paper and found the need to print tables in Markdown format. The syntax is relatively simple, but programmatically generating these tables is tedious, and requires a lot of string-formatting which can get irritating.

To ease the process, I scripted up a new one-hour project called PyTablePrinter, a Python library that converts data to markdown tables.

For instance, let’s say we have the following data:

data = [
    {
        "has_sequel": True,
        "author": "Margaret Atwood",
        "title": "Oryx & Crake",
        "rating": 5
    },
    {
        "author": "Kurt Vonnegut",
        "title": "Galapagos",
        "rating": 5
    }
]

We can import a new tableprinter and, simply by passing in the data to be formatted, we can generate a markdown table:

from tableprinter import *
tp = TablePrinter(data)
print tp.to_markdown()

You can supply extra arguments to the TablePrinter constructor to get different results. For instance, specify the order of columns to include:

TablePrinter(data, col_order=['title', 'author'])

Specifying column titles that have no corresponding datum in the dictionaries of data will result in empty columns (but will not fail to render):

TablePrinter(data, col_order=['title', 'rating', 'has_sequel' 'favorite'])
title rating has_sequel favorite
Oryx & Crake 5 True  
Galapagos 5    

You can also provide 2-tuples instead of a simple list for col_order. The first will be used as the lookup key, and the second string will be used as the table title.

TablePrinter(data, col_order=[
                    ("title", "The Title Of The Book"),
                    ("rating", "Rating"),
                    ("has_sequel", "Sequel?"),
                    ("favorite", "Favorite?")])
The Title Of The Book Rating Sequel? Favorite?
Oryx & Crake 5 True  
Galapagos 5    

You can even mix the two, with something like:

TablePrinter(data, col_order=[
                    ("title", "The Title Of The Book"),
                    "rating",
                    ("has_sequel", "Sequel?"),
                    "favorite"])

Writing this little library has already saved me tons of time. Hope it saves you some time too!

…oh, and of course, PyTablePrinter is available on GitHub and pypi:

pip install pytableprinter

Update February 18, 2016:

…and for my next trick…

The latest, neatest trick is the implementation of functions as columns as well. If you provide a 3-tuple and the last item is a function, you can run a function on each datum like so:

("rating percentage", "Rating %", lambda d: str(100. * d['rating']/5.0) + "%")

(This, of course, returns a percentage rating, using the rating field, as calculated out of a possible 5.)


def get_percent_rating(d):
    return str((100. * d['rating']) / 5.) + "%"

TablePrinter(data,
             col_order=[
                ("title", "The Title Of The Book"),
                ("rating percentage", "Rating %", get_percent_rating)
                ("has_sequel", "Sequel?"),
                "favorite"])
The Title Of The Book Rating Sequel? favorite
Oryx & Crake 100% True  
Galapagos 100%    
Written on January 1, 2016