How to convert a formated table in HTML to image PNG in COLAB?

1.5k Views Asked by At

After struggling trying to convert a dataframe to an image (png) directly, I finally could move the dataframe to another file structure (HTML) with the following code. The problem is that I couldn´t manage to export this HTML to an image, it just doesn't appear in colab (files) or in my drive root. Can anyone please help? Thank you very much.

#DATA AND FORMATTING
data = {'p': ['1','2','3'], 't': ['19:28','15:40','05:56'], 't': ['Mason','Luke','Flip']}  
df = pd.DataFrame(data)  
df

df2 = df.style.set_properties(**{'border':'0px', 'padding':'0px'})\
.set_properties(subset=['p'], **{'width': '10px', 'font-size': '12px', 'padding':'5px'})\
.set_properties(subset=['t'], **{'width': '10px','font-size': '15pt', 'font-weight': 'bold', 'color': 'gray', 'text-align': 'center'})
df2

#CONVERTING DATAFRAME TO HTML
from IPython.display import HTML
h = HTML(df2.to_html())
my_file = open('some_file.html', 'w')
my_file.write(h.data)
my_file.close()

#CONVERTING HTML TO PNG
!pip install --upgrade html2image
from html2image import Html2Image
hti = Html2Image(output_path='/content/drive/My Drive/')
hti.screenshot(html_file='some_file.html', save_as='blue_page.png')
0

There are 0 best solutions below