Kyrylo
Kyrylo

Reputation: 55

Transform html table to image in Python(other solves aren't working)

I need to save html table as image(argb, png), but all solves on stackoverflow aren't good for my project, because all of them working with os(cmd) or use api with limited number of uses. I need to save picture infinity number of times and this have to work in Jupiter Notebook.

How I can do this? Thanks!

Upvotes: 0

Views: 778

Answers (1)

Tiberiu C.
Tiberiu C.

Reputation: 3513

The simplest solution is to use a browser driver to take a picture of your html. Generaly you can take a picture of a remote page with pyppeteer :

import asyncio
import pyppeteer
async def main():
    browser = await pyppeteer.launch()
    page = await browser.newPage()
    await page.goto('https://example.com/')
    await page.screenshot({'path': './example.png'})
    await browser.close()
asyncio.get_event_loop().run_until_complete(main())

And you can also put into the goto call directly the content with a data url :

import asyncio
import pyppeteer
import base64
async def main():
    browser = await pyppeteer.launch()
    page = await browser.newPage()
    await page.goto('data:text/html,<table><tr><td>Some Cell</td><td>Some Other cell</td></tr></table>')
    await page.screenshot({'path': './screenshot.png'})
    await browser.close()
asyncio.get_event_loop().run_until_complete(main())

the picture generate by this looks like this :

Table Render

Ofcourse you need to install pyppeteer for that with in your virtual env:

$ python -m pip install pyppeteer


The same as above but written into a Jupyter Notebook here : https://gist.github.com/tiberiucorbu/6726e27a736ba7b07e3d0973e905e4ff

Upvotes: 1

Related Questions