Home > Software design >  Exporting a python dataframe text element to an image
Exporting a python dataframe text element to an image

Time:04-01

I want to print a data frame as a png image, and followed the following approach.

import pandas as pd
import dataframe_image as dfi

data = {'Type': ['Type 1', 'Type 2', 'Type 3', 'Total'], 'Value': [20, 21, 19, 60]}
df = pd.DataFrame(data)
dfi.export(df, 'table.png')

I however want to also print a date stamp above the table on the image - with the intention of creating a series of images on consecutive days. If possible I would also like to format the table with a horizontal line indicating the summation of values for the final 'Total' row.

Is this possible with the above package? Or is there a better approach to do this?

CodePudding user response:

You can add the line df.index.name = pd.Timestamp('now').replace(microsecond=0) to add the timestamp on the first row:

enter image description here

To add the line you can use .style.set_table_styles:

data = {'Type': ['Type 1', 'Type 2', 'Type 3'], 'Value': [20, 21, 19]}
df = pd.DataFrame(data)
df.index.name = pd.Timestamp('now').replace(microsecond=0)
df.loc[len(df)] = ['Total',df['Value'].sum()]
test = df.style.set_table_styles([{'selector' : '.row3','props' : [('border-top','3px solid black')]}])
dfi.export(test, 'table.png')

enter image description here

  • Related