How to Convert EML to PNG in Python

Cloudmersive
2 min readJul 6, 2020

Converting emails into images is a more challenging process than you might think at first look. EML format itself needs to be parsed into its constituent HTML, which you then have to render, and then use that to create your images. All this can take a very long time to set up (believe me I know). That’s why today I will be showing you how to skip all of that by applying a simple API.

Starting things off, we will run this command for pip install, which will bring in our client library into the project.

pip install cloudmersive-convert-api-client

And now we can instance our API, provide it with an API key, and use that to call our function with the EML file as an argument.

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.ConvertDocumentApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.try:# Convert Email EML file to PNG image arrayapi_response = api_instance.convert_document_eml_to_png(input_file)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_eml_to_png: %s\n" % e)

Ta-da! All done. Super easy, huh?

--

--

Cloudmersive

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