How to convert an EML Email File to PDF Document in Python

EML format certainly has its benefits in some situations, but who could argue that it is more convenient than a good old fashioned PDF? Today we delve into applying Python to the matter of converting between these two formats. If you were expecting a lengthy coding session, prepare to be surprised. The only work you will need to do is sign up for a free API and copy and paste a bit of code. Total time: 5 minutes. Let’s roll.

Install the API client first:

pip install cloudmersive-convert-api-client

Now toss your EML file into the convert_document_eml_to_pdf function, like so:

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.body_only = true # bool | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false. (optional)try:# Convert Email EML file to PDF documentapi_response = api_instance.convert_document_eml_to_pdf(input_file, body_only=body_only)pprint(api_response)except ApiException as e:print("Exception when calling ConvertDocumentApi->convert_document_eml_to_pdf: %s\n" % e)

And ba-da-bing ba-da-boom, you are done! Any EML file that you input into this function will return you a nice, neat PDF. And you better believe that’s not all that this library can do! Get yourself over to that documentation section to learn more.

Image for post
Image for post

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store