How to Convert Outlook Email to PDF using Python

In this easy tutorial, we will learn how to convert Outlook Email to PDF using Python along with detailed steps to configure the environment. You will see that in order to convert MSG to PDF Python based API can be used and you can use this example in any of the commonly available operating systems like Windows or Linux.

Steps to Convert Outlook Email to PDF using Python

  1. Establish the environment to use Aspose.Email for Python via .NET and Aspose.Words for Python via .NET in your application to convert Email to PDF in Python
  2. Create an instance of the MailMessage class object to load the source EML or MSG file from the disk or mail server
  3. Save the Email (MSG/EML) file to a memory stream in an intermediate MHTML format
  4. Use the LoadFormat as MHTML and create an object of the Document class to load the intermediate format MHTML memory stream
  5. By using the save method in Python perform the EML to PDF conversion

By using the above steps in Python MSG to PDF conversion application is developed where we will first access the EML or MSG file from the disk using the MailMessage class object, which is then followed by saving the email to a memory stream in an intermediate MHTML file format. In the subsequent steps, we will access the intermediate MHTML file using the Document class object and finally to convert EML to PDF Python save method will be used.

Code to Convert Outlook Email to PDF using Python

import aspose.email as ae
import aspose.words as aw
import io
# Path to the source EML/MSG
filePath = "C://SampleTestData//"
# Load the license in your application for converting EML to PDF
emltoPdfLicense = ae.License()
emltoPdfLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic")
# Set the Aspose.Words API license to convert MHTML to PDF
mhtmlToPDFLicense = aw.License()
mhtmlToPDFLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic")
# Access the source EML or MSG file from the disk
srcEmlMessage = ae.MailMessage.load(filePath + "Message.msg")
# Save the email file to a memory stream as an MHTML with default options
int_mhtml_Stream = io.BytesIO()
# Save the EML or MSG file to a stream in MHTML file format
srcEmlMessage.save(int_mhtml_Stream, ae.SaveOptions.default_mhtml)
# Seek the memory position to 0th index
int_mhtml_Stream.seek(0)
# Use the LoadOptions to set the load format to Mhtml
mhtmlLoadOptions = aw.loading.LoadOptions()
mhtmlLoadOptions.load_format = aw.LoadFormat.MHTML
# Create an instance of the Document class to load the MTHML file from Memory Stream
mhtmlDocument = aw.Document(int_mhtml_Stream, mhtmlLoadOptions)
# Close the MHTML Memory Stream
int_mhtml_Stream.close()
# Initialize the PdfSaveOptions class object to se the PDF options
pdfSaveOptions = aw.saving.PdfSaveOptions()
# Save the loaded MHTML file to a PDF using Python
mhtmlDocument.save(filePath + "SaveEmailAsPDF.pdf", pdfSaveOptions)
print ("EML converted to PDF file")

The above example exhibits in order to convert EML to PDF Python is used with the help of simple API calls and a well defined procedure. This example can be used for Email files in EML and MSG file formats. You can also customize the output PDF by setting properties like Compliance, ColorMode, JpegQuality and ImageCompression.

To convert MSG to PDF Python based API is explored in this article. If you are looking to split a PST file in Python, refer to the article on how to split PST File using Python.

 English