How to Convert an HTML String to a PNG Screenshot using Python
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
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 parameterstry:
# Convert HTML string to PNG screenshot
api_response = api_instance.convert_web_html_to_png(input)
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.