How to Convert an HTML String to a PNG Screenshot using Python

Cloudmersive
1 min readJul 29, 2022

If you want to fully render website images without having to generate the image yourself, there are a few avenues you can take. If you’re looking to include that operation as a service within your app, our HTML to PNG API can do the trick. With an HTML string as input, it will return a fully rendered PNG screenshot of the website in question — perfect for sharing across browsers and systems.

Below, I’ll walk you through structuring your API call using Python code examples. If you need code for a different programming language, our API console page contains code examples in more than a dozen others.

Let’s begin by running the following command to install the Python SDK:

pip install cloudmersive-convert-api-client

Now let’s include the imports:

from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint

After that, we can call the function. You can authenticate your API key in the first snippet (you can get an API key by registering a free account on our website):

# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.ConvertWebApi(cloudmersive_convert_api_client.ApiClient(configuration))
input = cloudmersive_convert_api_client.HtmlToPngRequest() # HtmlToPngRequest | HTML to PNG request parameters
try:
# Convert HTML string to PNG screenshot
api_response = api_instance.convert_web_html_to_png(input)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertWebApi->convert_web_html_to_png: %s\n" % e)

That’s all you need — no more code required.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.