Last active
October 17, 2022 15:24
How to Convert Outlook Email to Word using Python. For more details: https://kb.aspose.com/email/python/how-to-convert-outlook-email-to-word-using-python/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.email as ae | |
import aspose.words as aw | |
import io | |
# Path to the source files | |
filePath = "Y://SampleData//" | |
# Load the license in your application for converting EML to MHTML | |
emlLicense = ae.License() | |
emlLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
# Set the Aspose.Words license for converting MHTML to a DOCX file | |
wordsLicense = aw.License() | |
wordsLicense.set_license(filePath + "Conholdate.Total.Product.Family.lic") | |
# Load the source EML or MSG file from the disk or mail server | |
srcMessage = ae.MailMessage.load(filePath + "Message.msg") | |
# Save email to stream as a MHTML with default options | |
mhtml_Stream = io.BytesIO() | |
# Save the email to MHTML file to a memory stream | |
srcMessage.save(mhtml_Stream, ae.SaveOptions.default_mhtml) | |
# Reset the Memory stream position | |
mhtml_Stream.seek(0) | |
# Configure the LoadOptions to set the load format to Mhtml | |
loadOptions = aw.loading.LoadOptions() | |
loadOptions.load_format = aw.LoadFormat.MHTML | |
# Instantiate the Document class object to load the intermediate MTHML from MemoryStream | |
mhtmlDocument = aw.Document(mhtml_Stream, loadOptions) | |
# Close the MHTML stream now | |
mhtml_Stream.close() | |
# Save the loaded MHTML to DOCX in Python | |
mhtmlDocument.save(filePath + "SaveEmailAsDoc.docx") | |
print ("EML converted to Word file") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment