การจัดการไฟล์ OLM ของ Outlook for Mac
OLM (Outlook for Mac Archive) คือรูปแบบไฟล์ที่เกี่ยวข้องกับ Microsoft Outlook for Mac ใช้เพื่อเก็บสำรองและจัดเก็บข้อความอีเมล รายชื่อผู้ติดต่อ รายการปฏิทิน งาน และข้อมูล Outlook อื่น ๆ บนคอมพิวเตอร์ Mac ไฟล์ OLM ทำหน้าที่เป็นรูปแบบสำรองหรือเก็บถาวร ช่วยให้ผู้ใช้บันทึกข้อมูล Outlook for Mac ของตนเพื่ออ้างอิงหรือย้ายในอนาคต ต้องทราบว่าไฟล์ OLM มีเฉพาะสำหรับ Outlook for Mac และไม่เข้ากันกับรูปแบบไฟล์ PST (Personal Storage Table) ที่ใช้โดย Outlook บน Windows หากต้องการย้ายข้อมูล Outlook ระหว่างแพลตฟอร์มต่าง ๆ เครื่องมือแปลงไฟล์จะมีประโยชน์ Aspose.Email มีเครื่องมือเหล่านี้รวมถึงการเปิดอ่านและฟังก์ชันอื่น ๆ สำหรับทำงานกับไฟล์ OLM.
การเปิดไฟล์ Outlook OLM
ไฟล์รูปแบบ OLM สามารถเปิดได้สองวิธี:
- โดยใช้คอนสตรัคเตอร์
- โดยใช้เมธอดสเตติก ‘from_file’
เปิดไฟล์ Outlook OLM ด้วยคอนสตรักเตอร์ของ Python
เพื่อเปิดไฟล์ เรียกคอนสตรัคเตอร์ของ OlmStorage คลาสและส่งชื่อไฟล์เต็มหรือสตรีมเป็นอาร์กิวเมนต์ให้กับมัน:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage(fileName)
เปิดไฟล์ OLM ด้วยเมธอดคงที่ FromFile ของ Python
เพื่อเปิดไฟล์ ให้ใช้เมธอดสเตติก ‘from_file’ ของ OlmStorage คลาสและส่งชื่อไฟล์เต็มหรือสตรีมเป็นอาร์กิวเมนต์ให้กับมัน:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
การจัดการโฟลเดอร์และอีเมลในไฟล์ OLM
เข้าถึงโครงสร้างโฟลเดอร์แบบเรียกซ้ำ
API ของ Aspose.Email ให้คุณจำลองและแสดงโครงสร้างโฟลเดอร์ที่ดึงจากไฟล์ OLM โดยใช้ฟังก์ชัน print_all_folders ฟังก์ชันนี้รับ property folder_hierarchy ของ OlmStorage คลาส พร้อมระดับการเยื้องเป็นอินพุต และทำการทริเวิร์สโครงสร้างแบบเรียกซ้ำเพื่อพิมพ์ชื่อโฟลเดอร์แต่ละอันด้วยการเยื้องที่เหมาะสม.
ต่อไปนี้คือตัวอย่างโค้ดที่สาธิตวิธีใช้ฟังก์ชัน print_all_folders เพื่อแสดงโครงสร้างโฟลเดอร์จากไฟล์ OLM:
import aspose.email as ae
def print_all_folders(folder_hierarchy, indent):
for folder in folder_hierarchy:
print(f"{indent}{folder.name}")
print_all_folders(folder.sub_folders, indent + "-")
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage(fileName)
print_all_folders(olm.folder_hierarchy, "")
ตัวอย่างโค้ดข้างต้นมีจุดประสงค์เพื่อแสดงโครงสร้างโฟลเดอร์ของไฟล์ OLM ผ่านฟังก์ชันเรียกซ้ำในรูปแบบที่เป็นระเบียบและอ่านง่ายขึ้น.
เข้าถึงโครงสร้างโฟลเดอร์โดยตรง
Aspose.Email ยังทำให้สามารถเข้าถึงโครงสร้างโฟลเดอร์จากไฟล์ OLM โดยตรงด้วยเมธอด get_folders() ของ OlmStorage คลาส.
ต่อไปนี้คือตัวอย่างโค้ดสำหรับเข้าถึงโครงสร้างโฟลเดอร์โดยตรง:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folders = olm.get_folders()
ดึงโฟลเดอร์ตามชื่อ
สามารถดึงโฟลเดอร์ใดก็ได้โดยชื่อโดยใช้เมธอด get_folder(name, ignore_case) ของ OlmStorage คลาส เมธอดนี้ต้องการพารามิเตอร์ชื่อโฟลเดอร์และความไวต่อการพิมพ์.
นี่คือตัวอย่างโค้ดที่แสดงวิธีดึงโฟลเดอร์โดยชื่อ:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
รายการอีเมลจากไฟล์ Outlook OLM
ไลบรารี Aspose.Email สามารถใช้เพื่ออ่านและแยกข้อความอีเมลจากไฟล์ Outlook for Mac (OLM) คุณสามารถดึงรายการอีเมลโดยใช้เมธอดต่อไปนี้ของ OlmFolder คลาสที่แสดงโฟลเดอร์:
- enumerate_messages() - วนซ้ำผ่านแต่ละข้อความอีเมลในโฟลเดอร์ เมธอดนี้คืนข้อความเป็นอินสแตนซ์ของ OlmMessageInfo คลาสที่ให้ข้อมูลพื้นฐานเกี่ยวกับแต่ละข้อความอีเมล เช่น หัวเรื่อง ผู้ส่ง วันที่ เป็นต้น.
- enumerate_mapi_messages() - ยังวนซ้ำผ่านแต่ละข้อความอีเมลในโฟลเดอร์ แต่ในกรณีนี้จะคืนข้อความเป็นอินสแตนซ์ของ MapiMessage คลาสที่แสดงข้อความอีเมลในรูปแบบที่ละเอียดและเฉพาะเจาะจงตาม MAPI มากขึ้น มันให้การเข้าถึงคุณสมบัติและรายละเอียดหลากหลายของข้อความอีเมล เพื่อการประมวลผลที่ซับซ้อนและเฉพาะทางมากขึ้น.
ตัวอย่างโค้ดด้านล่างแสดงวิธีแยกหัวข้ออีเมลพื้นฐานและบันทึกข้อความอีเมลโดยละเอียดจากไฟล์ Outlook OLM โดยใช้ enumerate_messages() เพื่อดึงหัวข้อและ enumerate_mapi_messages() เพื่อบันทึกข้อความเป็นไฟล์ .msg.
แยกข้อมูลอีเมลพื้นฐานโดยใช้เมธอด ’enumerate_messages()’
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
for message_info in folder.enumerate_messages():
print(message_info.subject)
บันทึกข้อความอีเมลโดยละเอียดโดยใช้เมธอด ’enumerate_mapi_messages()’
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
for msg in folder.enumerate_mapi_messages():
msg.save(f"{msg.subject}.msg")
ดึงจำนวนข้อความทั้งหมดในโฟลเดอร์ OLM
นี้ OlmFolder คลาสให้ตัวเลือกเพิ่มเติมด้วยเมธอดต่อไปนี้:
- has_messages - รับค่าที่บ่งบอกว่าโฟลเดอร์ปัจจุบันมีข้อความหรือไม่.
- message_count - รับจำนวนข้อความ.
ตัวอย่างโค้ดด้านล่างแสดงวิธีใช้เมธอดเหล่านี้:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
if folder.has_messages:
print(f"Message count: {folder.message_count}")
รับหรือกำหนดวันที่แก้ไขสำหรับข้อความ Outlook OLM
Aspose.Email ทำให้สามารถดึงข้อมูลเวลาการแก้ไขล่าสุดของข้อความอีเมลได้ Property modified_date ของ OlmMessageInfo คลาสนี้แสดงวันที่และเวลาที่ข้อความถูกแก้ไขครั้งล่าสุด.
นี่คือตัวอย่างที่แสดงการใช้งานของ property:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
for message_info in folder.enumerate_messages():
modifiedDate = message_info.modified_date
การแยกเนื้อหา OLM
แยกอีเมลจากไฟล์ Outlook OLM
คุณสามารถดึงข้อมูลข้อความ MAPI จริงจากที่จัดเก็บอีเมลโดยใช้เมธอด extract_mapi_message(message_info) ของ OlmStorage คลาส เมธอดนี้แยกข้อความ MAPI จากที่จัดเก็บตาม message_info ที่ระบุ.
ตัวอย่างโค้ดด้านล่างแสดงวิธีใช้เมธอดนี้:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
for message_info in folder.enumerate_messages():
msg = olm.extract_mapi_message(message_info)
แยกข้อความ Outlook จากไฟล์ OLM ตามตัวระบุ
เพื่อเข้าถึงข้อมูลข้อความ MAPI คุณสามารถใช้ property entry_id เพื่อรับตัวระบุเฉพาะ (Entry ID) ของข้อความโดยใช้ OlmMessageInfo คลาส จากนั้นคุณสามารถใช้เมธอด extract_mapi_message(id) ของ OlmStorage คลาส โดยส่ง Entry ID เป็นพารามิเตอร์เพื่อดึงข้อความ MAPI ที่เชื่อมโยงกับ Entry ID นั้น ตัวอย่างโค้ดต่อไปนี้แสดงการใช้คุณลักษณะเหล่านี้:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage.from_file(fileName)
folder = olm.get_folder("Inbox", True)
for message_info in folder.enumerate_messages():
msg = olm.extract_mapi_message(message_info.entry_id)
การจัดการโครงสร้างโฟลเดอร์ OLM
ดึงเส้นทางโฟลเดอร์ในไฟล์ Outlook OLM
Aspose.Email อนุญาตให้คุณรับเส้นทางหรือที่ตั้งแบบลำดับชั้นของโฟลเดอร์ภายในไฟล์ Outlook OLM API ให้ property path ของ OlmFolder คลาสที่คืนค่าพาธของโฟลเดอร์ ตัวอย่างโค้ดต่อไปนี้แสดงการใช้คุณสมบัตินี้:
import aspose.email as ae
def print_path(storage, folders):
for folder in folders:
# print the current folder path
print(folder.path)
if folder.sub_folders:
print_path(storage, folder.sub_folders)
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage(fileName)
print_path(olm, olm.folder_hierarchy)
นับรายการในโฟลเดอร์ Outlook OLM
Aspose.Email ให้ความสามารถในการนับจำนวนข้อความอีเมลทั้งหมดที่อยู่ในโฟลเดอร์เฉพาะของไฟล์ Outlook OLM โดย property message_count ของ OlmFolder คลาสนี้คืนค่าจำนวนรายการทั้งหมด (ข้อความอีเมล) ที่เก็บอยู่ในโฟลเดอร์เฉพาะในไฟล์ OLM ตัวอย่างโค้ดต่อไปนี้แสดงการใช้คุณสมบัตินี้:
import aspose.email as ae
def print_message_count(folders):
for folder in folders:
print(f"Message Count [{folder.name}]: {folder.message_count}")
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage(fileName)
print_message_count(olm.folder_hierarchy)
รับจำนวนรายการทั้งหมดในไฟล์ Outlook OLM
เมธอด get_total_items_count() ของ OlmStorage คลาสคืนค่าจำนวนรายการข้อความทั้งหมดที่อยู่ในที่จัดเก็บ OLM ตามที่แสดงในตัวอย่างโค้ดด้านล่าง:
import aspose.email as ae
fileName = "my.olm"
olm = ae.storage.olm.OlmStorage(fileName)
count = olm.get_total_items_count()
การจัดการหมวดหมู่ Outlook
ดึงสีของหมวดหมู่ Outlook
ด้วย Aspose.Email คุณสามารถดึงและใช้สีของหมวดหมู่ที่เชื่อมโยงกับรายการ Outlook ที่เก็บในไฟล์ OLM ได้อย่างง่ายดาย OlmItemCategory คลาสนี้ทำให้คุณเข้าถึงชื่อหมวดหมู่และสีที่แสดงในรูปแบบฮेकซะเดซิล OlmStorage class features the get_categories() method for retrieving a list of categories from OLM storage. By implementing the code sample below, you can effortlessly retrieve all used categories from an OML storage file and access the category name along with its color.
import aspose.email as ae
with ae.storage.olm.OlmStorage.from_file("storage.olm") as olm:
categories = olm.get_categories()
for category in categories:
print(f"Category name: {category.name}")
# Color is represented as a hexadecimal value: #rrggbb
print(f"Category color: {category.color}")
นอกจากนี้คุณยังสามารถดึงสีของหมวดหมู่ที่เชื่อมโยงกับข้อความเฉพาะโดยการวนลูปผ่านข้อความในโฟลเดอร์และเข้าถึงสีหมวดหมู่ที่สอดคล้องตามชื่อหมวดหมู่
import aspose.email as ae
olm = ae.storage.olm.OlmStorage.from_file("storage.olm")
categories = olm.get_categories()
folder = olm.get_folder("Inbox", True)
for msg in folder.enumerate_mapi_messages():
if msg.categories is not None:
for msg_category in msg.categories:
print(f"Category name: {msg_category}")
category_color = next((c.color for c in categories if c.name.lower() == msg_category.lower()), None)
if category_color is not None:
print(f"Category color: {category_color}")