Export Pandas Styled Table To Image File
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 no
Solution 1:
As mentioned in the comments, you can use the render
property to obtain an HTML of the styled table:
html = styled_table.render()
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')
Solution 2:
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')
Post a Comment for "Export Pandas Styled Table To Image File"