Package: df2img
df2img: Save a Pandas DataFrame as imageWhat is it all about?Have you ever tried to save apd.DataFrameinto an image file? This is not a straightforward process at all. Unfortunately,pandasitself doesn't provide this functionality out of the box.df2imgtries to fill the gap. It is a Python library that greatly simplifies the process of saving apd.DataFrameinto an image file (e.g.pngorjpg).It is a wrapper/convenience function in order to create aplotlyTable. That is, one can useplotly's styling function to format the table.Dependenciesdf2imghas a limited number of dependencies, namelypandasplotlykaleidoDocumentationAn extensive documentation is available athttps://df2img.dev.QuickstartYou can install the package viapip.pipinstalldf2imgUsingpoetryorpdm?poetryadddf2img
pdmadddf2imgLet's create a simplepd.DataFramewith some dummy data:importpandasaspdimportdf2imgdf=pd.DataFrame(data=dict(float_col=[1.4,float("NaN"),250,24.65],str_col=("string1","string2",float("NaN"),"string4"),),index=["row1","row2","row3","row4"],)float_colstr_colrow11.40string1row2NaNstring2row3250.00NaNrow424.65string4BasicsSavingdfinto a png-file now takes just two lines of code including some styling out of the box.First, we create aplotlyfigure.Second, we save the figure to disk.fig=df2img.plot_dataframe(df,fig_size=(500,140))df2img.save_dataframe(fig=fig,filename="plot1.png")FormattingYou can control the settings for the header row via thetbl_headerinput argument. This accepts a regulardict. Thisdictcan comprise various key/value pairs that are also accepted byplotly. All available key/value pairs can be seen atplotly's website athttps://plotly.com/python/reference/table/#table-header.Let's set the header row in a different color and size. Also, let's set the alignment to "left".fig=df2img.plot_dataframe(df,tbl_header=dict(align="left",fill_color="blue",font_color="white",font_size=14,),fig_size=(500,140),)Controlling the table body (cells) is basically the same. Just use thetbl_cellsinput argument, which happens to be adict, too.
Seehttps://plotly.com/python/reference/table/#table-cellsfor all the possible key/value pairs.Let's print the table cell values in yellow on a green background and align them "right".fig=df2img.plot_dataframe(df,tbl_cells=dict(align="right",fill_color="green",font_color="yellow",),fig_size=(500,140),)You can alternate row colors for better readability by using therow_fill_colorinput argument. Using HEX colors is also possible:fig=df2img.plot_dataframe(df,row_fill_color=("#ffffff","#d7d8d6"),fig_size=(500,140),)Setting the title will be controlled via thetitleinput argument. You can find the relevant key/value pairs here:https://plotly.com/python/reference/layout/#layout-title.Let's put the title in a different font and size. In addition, we can control the alignment via thexkey/value pair. It sets the x (horizontal) position in normalized coordinates from "0" (left) to "1" (right).fig=df2img.plot_dataframe(df,title=dict(font_color="darkred",font_family="Times New Roman",font_size=24,text="This is a title starting at the x-value x=0.1",x=0.1,xanchor="left",),fig_size=(500,140),)You can also control relative column width via thecol_widthargument. Let's set the first column's width triple the width of the third column and the second column's width double the width of the third column.fig=df2img.plot_dataframe(df,col_width=[3,2,1],fig_size=(500,140),)Contributing to df2imgIf you consider to contribute todf2img, please read theContributing to df2imgsection in the documentation. This document is supposed to guide you through the whole process.