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

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

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

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

Python MSG から PDF への変換アプリケーションで上記の手順を使用することにより、最初に MailMessage クラス オブジェクトを使用してディスクから EML または MSG ファイルにアクセスし、その後、電子メールを中間のメモリ ストリームに保存することによって開発されます。 MHTML ファイル形式。以降の手順では、Document クラス オブジェクトを使用して中間 MHTML ファイルにアクセスし、最後に EML を PDF に変換するために Python save メソッドを使用します。

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

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

上記の例では、EML を PDF に変換するために、Python が単純な API 呼び出しと明確に定義された手順を使用して使用されています。この例は、EML および MSG ファイル形式の電子メール ファイルに使用できます。 Compliance、ColorMode、JpegQuality、ImageCompression などのプロパティを設定して、出力 PDF をカスタマイズすることもできます。

MSG を PDF に変換する Python ベースの API については、この記事で説明します。 Python で PST ファイルを分割する場合は、Pythonを使用してPSTファイルを分割する方法 の記事を参照してください。

 日本語