What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To create an image preview of an HTML email with Python, load the markup into a Playwright page and save a screenshot. Use page.set_content() for an HTML string or file contents, then call page.screenshot(). This produces a browser rendering—not proof of how the email will look in Gmail, Outlook, Apple Mail, or another mail client.

What you need

  • Python and the Playwright Python package in your project environment.
  • A Playwright browser installation for the engine you plan to use. Playwright supports Chromium, Firefox, and WebKit; install and run the engine appropriate to your preview workflow.
  • An HTML email file, or a string containing the HTML markup.

Playwright’s official Python guide covers installation and both synchronous and asynchronous APIs: Playwright for Python.

Generate a screenshot from an HTML file

This synchronous example reads the file with an explicit UTF-8 encoding, renders it in Chromium, and saves a full-page PNG:

from pathlib import Path
from playwright.sync_api import sync_playwright

html = Path("email.html").read_text(encoding="utf-8")

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page(viewport={"width": 600, "height": 900})
    page.set_content(html)
    page.screenshot(path="preview.png", full_page=True)
    browser.close()
  1. Save the email markup as email.html, or change the path in the script.
  2. Run the script in the Python environment where Playwright and its browser are installed.
  3. Open preview.png to inspect the rendered page.

The 600-by-900 viewport is an illustrative choice, not an official email standard or a universal recommendation. Select dimensions that suit the preview you want, and keep them consistent when comparing screenshots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The APIs used here are documented in the Page API for set_content() and the Playwright screenshot guide.

Choose what to capture

Visible viewport

Use page.screenshot(path="preview.png") to save the currently visible page area. This is useful when you want to inspect a specific viewport rather than the entire message.

Full scrollable page

Set full_page=True to capture the full scrollable page as a tall image: page.screenshot(path="preview.png", full_page=True). It shows content beyond the initial viewport, but it is not a substitute for checking the layout at several viewport sizes.

One email element

Use a locator screenshot to capture a specific matching element, such as the main email container:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
page.locator(".email-container").screenshot(path="email-container.png")

This is useful when the HTML includes surrounding page content you do not want in the image. The selector must match an element in the rendered page.

Screenshot bytes

If you need to process the image in Python instead of saving it directly, the screenshot API can return image bytes. See the screenshot guide for the API options.

Use the asynchronous API in an asyncio project

When the surrounding application uses asyncio, use Playwright’s asynchronous API and await browser, page, content, screenshot, and close operations:

from pathlib import Path
from playwright.async_api import async_playwright

async def main():
    html = Path("email.html").read_text(encoding="utf-8")

    async with async_playwright() as p:
        browser = await p.chromium.launch()
        page = await browser.new_page(viewport={"width": 600, "height": 900})
        await page.set_content(html)
        await page.screenshot(path="preview.png", full_page=True)
        await browser.close()

Call main() from your existing async application. The Playwright Python guide describes when to use the async API: Playwright for Python.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose an engine and keep previews comparable

Playwright can launch Chromium, Firefox, or WebKit. Select the engine you want to inspect; the official documentation does not establish that any one engine represents a particular email client.

For repeatable visual review, keep the operating environment, browser version, engine, settings, headless mode, and viewport consistent. Playwright notes that these factors, along with hardware, can cause rendering variation. Its visual comparison workflow can compare a reference screenshot with later output and apply a stylesheet to filter volatile content: Visual comparisons and snapshots.

Account for external resources

If the email HTML references remote images, fonts, stylesheets, or other resources, they can affect the rendered preview. When an image or style is missing, inspect the HTML’s resource URLs and whether those resources are available to the browser. A screenshot of the page only shows what the browser rendered under the conditions of that run.

Troubleshooting

  • The script cannot import Playwright: Install the Playwright Python package in the same Python environment used to run the script, then follow the official installation guide to install a browser.
  • Browser launch fails: Confirm that the browser installation completed for the engine selected in the code. If you switch from Chromium to Firefox or WebKit, install that engine too.
  • The saved image is blank or incomplete: Check that the HTML string or file contains the expected markup, then inspect the page and the availability of remote images, fonts, and stylesheets. A browser preview cannot show a resource that did not load.
  • The screenshot cuts off content: Use full_page=True for the full scrollable page, or capture a matching element with a locator if you need a specific region.
  • Two screenshots differ unexpectedly: Compare them using the same browser engine and version, operating environment, settings, headless mode, hardware where feasible, and viewport. Consider filtering changing content in a visual comparison stylesheet.
  • The preview does not match an email client: Playwright renders a browser page. The documented workflow does not establish that the result matches Gmail, Outlook, Apple Mail, or any other mail client’s rendering. Check the message separately in the clients you support.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

For a hosted screenshot from one GET request, ScreenshotNeo accepts a URL and returns an image or PDF. That is different from passing raw HTML to page.set_content(); host the email preview at a URL if you want to capture it this way. See the ScreenshotNeo API documentation for parameters and response details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo removes known cookie/consent banners, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server provides screenshot tools for AI agents, including Claude, Cursor, and other MCP clients. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

Frequently Asked Questions

Does Playwright’s screenshot prove my email works in Gmail or Outlook?

No. It captures a browser rendering of the HTML. It does not establish how a mail client renders the message.

Can I save a screenshot without writing it directly to a file?

Yes. Playwright’s screenshot API can return image bytes for post-processing; consult the screenshot guide for the API options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.