pythontabular

Creating publication quality tables in python


I'd like to create publication quality tables for output as svg or jpg or png images using python.

I'm familiar with the texttable module which produces nice text tables but if I have for example

data = [['Head 1','Head 2','Head 3'],['Sample Set Type 1',12.8,True],['Sample Set Type 2',15.7,False]]

and I wanted to produce something that looked like

table image

Is there a module I can turn to, or can you point me to a process for going about it?


Solution

  • There are large amounts of possibilities for you.

    You can convert a Pandas dataframe to Latex as per https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_latex.html

    You can also use Tabular to output latex source as per http://en.wikibooks.org/wiki/LaTeX/Tables

    You can use ReportLab, as per Python reportlab inserting image into table

    You could also just write an HTML table file and style it with css.

    with open("example.html", "w") as of:
        of.write("<html><table>")
        for index, row in enumerate(data):
            if index == 0:
                 of.write("<th>")
            else:
                 of.write("<tr>")
    
            for cell in row:
                 of.write("<td>" + cell + "</td>")
            if index == 0:
                 of.write("</th>")
            else:
                 of.write("</tr>")
    
        of.write("</table></html>")
    

    You can do something similar with Latex tables as an output.

    Self edit, years later: Rich's tables are great for tables in the shell: https://rich.readthedocs.io/en/stable/tables.html

    enter image description here