วิธีแปลงอีเมล Outlook เป็น PDF โดยใช้ Python

ในบทช่วยสอนง่ายๆ นี้ เราจะเรียนรู้วิธี แปลง Outlook Email เป็น PDF โดยใช้ Python พร้อมกับขั้นตอนโดยละเอียดในการกำหนดค่าสภาพแวดล้อม คุณจะเห็นว่าในการแปลง MSG เป็น PDF Python สามารถใช้ API ได้ และคุณสามารถใช้ตัวอย่างนี้ในระบบปฏิบัติการใดๆ ที่มีอยู่ทั่วไป เช่น Windows หรือ Linux

ขั้นตอนในการแปลงอีเมล Outlook เป็น PDF โดยใช้ Python

  1. สร้างสภาพแวดล้อมเป็น ใช้ Aspose.Email สำหรับ Python ผ่าน .NET และ Aspose.Words สำหรับ Python ผ่าน .NET ในแอปพลิเคชันของคุณเพื่อแปลงอีเมลเป็น PDF ใน Python
  2. สร้างอินสแตนซ์ของออบเจกต์คลาส MailMessage เพื่อโหลดไฟล์ EML หรือ MSG ต้นทางจากดิสก์หรือเมลเซิร์ฟเวอร์
  3. บันทึกไฟล์อีเมล (MSG/EML) ไปยังสตรีมหน่วยความจำในรูปแบบ MHTML ระดับกลาง
  4. ใช้ LoadFormat เป็น MHTML และสร้างวัตถุของคลาส Document เพื่อโหลดสตรีมหน่วยความจำ MHTML รูปแบบกลาง
  5. โดยใช้วิธีการบันทึกใน Python ทำการแปลง EML เป็น PDF

โดยใช้ขั้นตอนข้างต้นในแอปพลิเคชันการแปลง Python MSG เป็น PDF ได้รับการพัฒนาโดยที่เราจะเข้าถึงไฟล์ EML หรือ MSG จากดิสก์ก่อนโดยใช้วัตถุคลาส MailMessage ซึ่งตามด้วยการบันทึกอีเมลไปยังสตรีมหน่วยความจำในสื่อกลาง รูปแบบไฟล์ MHTML ในขั้นตอนต่อมา เราจะเข้าถึงไฟล์ MHTML ระดับกลางโดยใช้วัตถุคลาส Document และสุดท้ายจะใช้วิธีบันทึก EML เป็น PDF Python

รหัสเพื่อแปลงอีเมล Outlook เป็น PDF โดยใช้ 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")

การแสดงตัวอย่างข้างต้นเพื่อ แปลง EML เป็น PDF Python ถูกนำมาใช้ด้วยความช่วยเหลือของการเรียก API อย่างง่ายและขั้นตอนที่กำหนดไว้อย่างดี ตัวอย่างนี้สามารถใช้กับไฟล์อีเมลในรูปแบบไฟล์ EML และ MSG คุณยังสามารถปรับแต่งเอาต์พุต PDF ได้ด้วยการตั้งค่าคุณสมบัติต่างๆ เช่น Compliance, ColorMode, JpegQuality และ ImageCompression

หากต้องการ แปลง MSG เป็น PDF ที่ใช้ Python มีการสำรวจในบทความนี้ หากคุณต้องการแยกไฟล์ PST ใน Python โปรดดูบทความใน วิธีแยกไฟล์ PST โดยใช้ Python

 ไทย