39

The code below when run in jupyter notebook renders a table with a colour gradient format that I would like to export to an image file.

The resulting 'styled_table' object that notebook renders is a pandas.io.formats.style.Styler type.

I have not been able to find a way to export the Styler to an image.

I hope someone can share a working example of an export, or give me some pointers.

import pandas as pd
import seaborn as sns

data = {('count', 's25'): 
       {('2017-08-11', 'Friday'): 88.0,
        ('2017-08-12', 'Saturday'): 90.0,
        ('2017-08-13', 'Sunday'): 93.0},
        ('count', 's67'): 
       {('2017-08-11', 'Friday'): 404.0,
        ('2017-08-12', 'Saturday'): 413.0,
        ('2017-08-13', 'Sunday'): 422.0},
        ('count', 's74'): 
       {('2017-08-11', 'Friday'): 203.0,
        ('2017-08-12', 'Saturday'): 227.0,
        ('2017-08-13', 'Sunday'): 265.0},
        ('count', 's79'): 
       {('2017-08-11', 'Friday'): 53.0,
        ('2017-08-12', 'Saturday'): 53.0,
        ('2017-08-13', 'Sunday'): 53.0}}

table = pd.DataFrame.from_dict(data)
table.sort_index(ascending=False, inplace=True)

cm = sns.light_palette("seagreen", as_cmap=True)
styled_table = table.style.background_gradient(cmap=cm)
styled_table

enter image description here

3

2 Answers 2

26

As mentioned in the comments, you can use the to_html() method to obtain an HTML of the styled table (in old versions it was render()):

html = styled_table.to_html()

You can then use a package that converts html to an image. For example, IMGKit: Python library of HTML to IMG wrapper. Bear in mind that this solution requires the installation of wkhtmltopdf, a command line tool to render HTML into PDF and various image formats. It is all described in the IMGKit page.

Once you have that, the rest is straightforward:

import imgkit
imgkit.from_string(html, 'styled_table.png')
7
  • I'm getting this error If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - http://wkhtmltopdf.org do you know how can I fix it?
    – Enzo Dtz
    Commented Jan 12, 2019 at 4:57
  • 1
    @EnzoDtz it seems that either wkhtmltopdf is not installed, or that it is not accessible via the system path. You need to be able in a command line to enter e.g. wkhtmltopdf --help and see the help menu.
    – Shovalt
    Commented Jan 13, 2019 at 7:54
  • 1
    If others don't want to deal with installs, @Shovalt's process is available inside your browser with active Jupyter sessions launched from here, taking advantage of the MyBinder.org system.
    – Wayne
    Commented Jun 18, 2019 at 16:04
  • 1
    that was the answer. My jupyter was being run from a service and i used os.system(echo $PATH) and saw there was none despite seeing it myself with which(wkhtmltopdf). So yeah, once I added environmentPath... yada yada to my .service file, jupyter picked up the path correctly. Commented May 18, 2021 at 14:36
  • 1
    Styler.render is deprecated since pandas 1.4.0, use Styler.to_html (from pandas 1.3.0 onward) instead. Commented Oct 13, 2023 at 18:51
25

You can use dexplo's dataframe_image from https://github.com/dexplo/dataframe_image. After installing the package, it also lets you save styler objects as images like in this example from the README:

import numpy as np
import pandas as pd
import dataframe_image as dfi

df = pd.DataFrame(np.random.rand(6,4))
df_styled = df.style.background_gradient()

dfi.export(df_styled, 'df_styled.png')
4
  • The previous answer was losing a lot of the formatting, this solution worked perfectly instead. Side note: I had to upgrade pip before I was able to install this package with pip install --upgrade pip. Commented May 5, 2021 at 9:59
  • As of 2021, this should be the top answer. Compared to the imgkit solution, it mimics Jupyter's style and doesn't require external dependencies.
    – Thrastylon
    Commented Dec 13, 2021 at 12:09
  • 1
    The doesn't render the LaTeX in my styled DataFrame. Commented Aug 31, 2022 at 14:53
  • As alluded to by @James Hirschorn, dataframe_image seems designed to work only with the HTML representation of Styler (which is relevant to OP since Jupyter uses the HTML), not any other output method currently available (e.g., Excel, LaTeX). The way to get those things to an image will be very different from the way to get HTML to an image. One option for pdfLaTeX is to then use, say, pdf2image.convert_from_path(...) to get a list of pillow images and image.save(filepath, 'PNG') to save each one. Commented Oct 13, 2023 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.