如何使用 Python 将 Outlook 电子邮件转换为 TIFF

在这个小主题中,我们将按照设置环境的详细步骤,重点介绍如何使用 Python Outlook Email 转换为 TIFF。为了将 EML 转换为 TIFF 基于 Python 的 API 提供了一个简单的 API 接口,可以在 Windows 或 Linux 等操作系统中的任何 Python 配置环境中使用。

使用 Python 将 Outlook 电子邮件转换为 TIFF 的步骤

  1. 在您的应用程序中将环境设置为 通过 .NET 使用 Aspose.Email for Python通过 .NET 的 Aspose.Words for Python 以在 Python 中将电子邮件转换为 TIFF
  2. 创建 MailMessage 类对象以加载 MSG 或 EML 格式的电子邮件文件
  3. 将内存流中访问的电子邮件 (EML/MSG) 文件保存为 MHTML 格式
  4. 将 LoadFormat 设置为 MHTML 并实例化 Document 类对象以加载在前面步骤中创建的中间 MHTML
  5. 使用 Python 中的 save 方法执行 EML 到 TIFF 的转换

通过执行上述步骤并使用 Python 中的简单代码,可以开发电子邮件到 TIFF 转换器应用程序。我们将首先使用 MailMessage 类的实例从磁盘加载源 MSG 或 EML 文件,然后将其保存到内存流中的中间 MHTML 文件中。随后,通过使用 Document 类的一个实例,MHTML 将被加载并使用 Save 方法作为 TIFF 文件保存在磁盘上。

使用 Python 将 Outlook 电子邮件转换为 TIFF 的代码

import aspose.email as ae
import aspose.words as aw
import io
# Path to load the source files
srcFilePath = "Y://TestData//"
# Load the license in your application for converting MSG/EML to MHTML
emlFileLicense = ae.License()
emlFileLicense.set_license(srcFilePath + "Conholdate.Total.Product.Family.lic")
# Load the Aspose.Words license in your application for converting MHTML to TIFF
wordsFileLicense = aw.License()
wordsFileLicense.set_license(srcFilePath + "Conholdate.Total.Product.Family.lic")
# Open the source MSG or EML file from the disk
srcMessageFile = ae.MailMessage.load(srcFilePath + "Message.msg")
# Save the document to an intermediate stream as an MHTML with default options
mhtml_int_Stream = io.BytesIO()
# Save the email to stream
srcMessageFile.save(mhtml_int_Stream, ae.SaveOptions.default_mhtml)
# seek the position of the Memory stream to the brginning
mhtml_int_Stream.seek(0)
# Set the LoadOptions to Load the Mhtml
loadOptions = aw.loading.LoadOptions()
loadOptions.load_format = aw.LoadFormat.MHTML
# Initialize the Document class object to load the intermediate MTHML from MemoryStream
mhtmlToTiffDocument = aw.Document(mhtml_int_Stream, loadOptions)
# Close the MHTML stream to avoid a memory leak
mhtml_int_Stream.close()
# Save the MHTML to TIFF using Python
mhtmlToTiffDocument.save(srcFilePath + "SaveEmailAsDoc.tiff")
print ("EML converted to Tiff file")

为了将 EML 转换为 TIFF,上面的示例中引用了基于 Python 的 API,公开了简单的功能和明确定义的步骤。这是一个多步骤过程,在第一步中,我们将在内存流中执行 EML 或 MSG 文件到中间 MHTML 文件的转换。在最后一步中,中间 MHTML 被呈现为 TIFF 文件并保存在磁盘上。

在此示例中,我们了解了如何使用基于 Python* 的 API *将 Outlook 电子邮件转换为 TIFF。如果您有兴趣了解如何创建 Mapi 联系人,请参阅 如何使用 Python 创建 Mapi 联系人 上的文章。

 简体中文