How to Convert HTML to PNG in Python
1 min readMar 13, 2024
Converting HTML to PNG saves a static iteration of a web page. We can easily make our conversion in Python with a free, low-code API call.
To start, let’s install the client SDK with pip install:
pip install cloudmersive-convert-api-client
Next, let’s add the below imports and call the function. Our HTML file path goes in the input_file
variable:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# 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.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Convert HTML document file to PNG image array
api_response = api_instance.convert_document_html_to_png(input_file)
pprint(api_response)
except ApiException as e:
print("Exception when calling ConvertDocumentApi->convert_document_html_to_png: %s\n" % e)
To satisfy the configuration.api_key
snippet, we’ll just need a free Cloudmersive API key. This will give us a limit of 800 API calls per month with zero additional commitments.
Nice and easy — no more code required.