0

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')
  • Html2Image uses existing web browsers (Chrome/Chromium for now) to generate images. Are you sure that Chrome/Chromium is available on colab? If Html2Image cannot find a suitable executable, it should raise an exception when you're instantiating it : "FileNotFoundError: Could not find a Chrome executable on this machine, please specify it yourself." – vgalin Feb 10 '22 at 14:05
  • Hi, @vgalin. First of all, thank you very much for your attention. I forgot to include that I used the solution proposed here [link](https://stackoverflow.com/questions/56829470/selenium-google-colab-error-chromedriver-executable-needs-to-be-in-path) to have chrome available, and it works well, so its safe to assume that Chrome is available on Colab. No error is thrown. So, I guess that is something regarding the following lines: html = df4_style.render() hti = Html2Image(output_path='/content/drive/My Drive/') image = hti.screenshot(html_str=html, save_as='red_page.png') image – Leonardo Paris Feb 11 '22 at 22:56
  • I'm currently taking a look at this issue. The problem seems to be related to running Chromium on Colab. For now html2image is only using commands to take screenshots using browsers. In the example you gave, the command is `chromium-browser --headless --screenshot=/content/drive/My Drive/blue_page.png --window-size=1920,1080 --default-background-color=0 --hide-scrollbars /tmp/html2image/some_file.html`. Although running it doesn't produce any image, logs or output... – vgalin Feb 12 '22 at 18:59
  • For now, I can only recommend you to try alternative methods to take screenshots, such as directly using Selenium. I'll let you know if I find a solution. – vgalin Feb 12 '22 at 19:11
  • I will study and figure out how to do it with selenium. I really appreciate your effort and attention. – Leonardo Paris Feb 13 '22 at 10:15
  • Hi, @vgalin! Just to know, could you find a solution? I´m still struggling to find one. Best regards! – Leonardo Paris Mar 13 '22 at 20:16
  • I wasn't able to find a workaround. However, I opened an issue for you on the html2image repo (https://github.com/vgalin/html2image/issues/62) so this won't be forgotten. I (or someone else) will hopefully find a solution one day, but it probably won't be quick. – vgalin Mar 14 '22 at 14:57

0 Answers0