Python を使用して Outlook 電子メールを Word に変換する方法

この簡単なチュートリアルでは、Python を使用して Outlook Email を Word に変換する方法と、環境を構成するための詳細な手順を学習します。 EML を DOCX に変換するには、Python ベースの API を使用できます。この例は、Windows や Linux などの一般的に利用可能なオペレーティング システムで使用できます。

Python を使用して Outlook 電子メールを DOCX に変換する手順

  1. アプリケーションで .NET 経由で Aspose.Email for Python を使用.NET 経由の Python 用 Aspose.Words に環境を設定し、Python でメールを Word に変換します
  2. ディスクまたはメール サーバーからソース MSG または EML ファイルをロードするための MailMessage クラス オブジェクトを作成します。
  3. 読み込まれた電子メール (EML/MSG) ファイルを MHTML 形式でメモリ ストリームにエクスポートします。
  4. LoadFormat を MHTML に初期化し、Document クラスのインスタンスを作成して、中間 MHTML ファイルをロードします。
  5. Python で save メソッドを使用して、EML から Word への変換を実行します。

前述の Python Email to Word 変換アプリケーションの手順に従って、MailMessage クラスのインスタンスを使用してディスクからソース EML または MSG ファイルをロードすることから始めます。その後、ロードされた電子メール ファイルをMHTML 形式のメモリ ストリーム。最後に、中間の MHTML が Document クラスのインスタンスを使用してメモリ ストリームから読み込まれ、電子メールが DOCX に変換されます。

Python を使用して Outlook 電子メールを DOCX に変換するコード

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")

上記の例では、EML を Word に変換するために Python* が使用され、シンプルな API インターフェイスと明確に定義された手順が使用されています。上記のコードは、MSG または EML 形式の電子メール ファイルに使用できます。読み込んだ MHTML を画像または PDF ファイル形式にエクスポートすることもできます。

このトピックでは、Python ベースの API を使用して Outlook メールを Docx に変換する方法を学びました。 MBOX ファイルを PST ファイルに変換する場合は、PythonでMBOXをPSTファイルに変換する方法 の記事を参照してください。

 日本語