Last week I suggested that it would be fun to scape a dashboard and send it as a email to an executive. Today we’ll do it using Python.
To start:
This example assumes you have a Gmail account.
Since you are using an app (Python) to send email, Google requires you to set up an “app password”. This is a security measure required by Google. Instructions here; you’ll use this app password (not your Gmail password) to authenticate and send the screenshot and .pdf from your Google Account.
I am assuming you’ll run this code in a cloud-based notebook. It’s very lightweight and you should be up and running pretty quickly.
First, install required packages:
pip install asyncio
pip install pyppeteer
pip install img2pdf
You’ll use `pyppeteer` to open a webpage and take a screenshot. `asyncio` basically delays that action (to give the page time to load: this is often not necessary but can be helpful depending on the dashboard source). `img2pdf` should be clear.
In the code below, you’ll need to fill in the url you want to scrape (`url_to_scrape`). I’ve pre-filled the name of the saved screenshot to be called 'example.png'.
import asyncio
from pyppeteer import launch
url_to_scrape=''
async def take_screenshot(url, output_path):
browser = await launch()
page = await browser.newPage()
await page.goto(url)
await asyncio.sleep(2) # Waits for (2) seconds
await page.screenshot({'path': output_path, 'fullPage': True})
await browser.close()
asyncio.get_event_loop().run_until_complete(take_screenshot(url_to_scrape, 'example.png'))
Once we have the screenshot we want, let’s convert it to a .pdf. You can update the pdf_name in the script below if you’d like: