การจัดการรายการปฏิทิน Outlook
นี้ MapiCalendar คลาสในไลบรารี Aspose.Email มีเมธอดและแอตทริบิวต์ต่าง ๆ เพื่อกำหนดค่าและจัดการรายการปฏิทิน ซึ่งช่วยให้การจัดการรายการปฏิทิน Outlook มีประสิทธิภาพมากขึ้น เพิ่มผลิตภาพและการจัดระเบียบ ด้านล่างเป็นส่วนต่าง ๆ ที่จะนำทางคุณผ่านแง่มุมต่าง ๆ ของการจัดการรายการปฏิทิน.
การสร้างและบันทึกรายการปฏิทิน
สร้างและบันทึกรายการปฏิทิน
โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างและบันทึกรายการปฏิทินในรูปแบบ ICS:
- กำหนดเส้นทางที่ไฟล์ปฏิทินของคุณจะถูกบันทึก.
- สร้างนัดหมายโดยการเริ่มต้นอ็อบเจ็กต์ใหม่ MapiCalendar อ็อบเจ็กต์ที่มีรายละเอียดเฉพาะเช่น สถานที่, หัวเรื่อง, คำอธิบาย, เวลาเริ่มต้นและสิ้นสุด.
- บันทึกนัดหมายที่สร้างเป็นไฟล์ ICS ในไดเรกทอรีที่ระบุ.
import aspose.email as ae
from datetime import datetime
data_dir = "path/to/data/directory/"
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2012, 4, 1),
datetime(2012, 5, 1))
calendar.save(data_dir + "CalendarItem_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
บันทึกรายการปฏิทินเป็น MSG
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกรายการปฏิทินเป็นไฟล์ MSG:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.save(data_dir + "CalendarItem_out.msg", ae.calendar.AppointmentSaveFormat.MSG)
ตั้งค่า Product ID สำหรับการแปลง MapiCalendar ไปเป็น ICS
ไลบรารี Aspose.Email ให้คุณบันทึก MapiCalendar (รายการปฏิทิน) เป็นไฟล์ ICS (iCalendar) ด้วยตัวเลือกเฉพาะ ด้านล่างเป็น คุณสมบัติ จะทำให้คุณรักษาเวลาและวันที่ต้นฉบับที่เชื่อมโยงกับรายการปฏิทินและตั้งค่า product identifier ในไฟล์ ICS ตัวอย่างเช่นเป็น "Foo Ltd" ตามลำดับ:
- ics_save_options.keep_original_date_time_stamp
- ics_save_options.product_identifier
รหัสผลิตภัณฑ์เป็นฟิลด์ที่ระบุซอฟต์แวร์หรือบริการที่สร้างไฟล์ ICS.
นี่คือตัวอย่างโค้ดที่ให้คุณตั้งค่า Product ID เมื่อบันทึก MapiCalendar เป็นไฟล์ ICS:
import aspose.email as ae
from datetime import datetime
mapi_calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2024, 4, 1),
datetime(2024, 4, 2))
ics_save_options = ae.mapi.MapiCalendarIcsSaveOptions()
ics_save_options.keep_original_date_time_stamp = True
ics_save_options.product_identifier = "Foo Ltd"
mapi_calendar.save("my.ics", ics_save_options)
อ่านและตั้งค่าอีดินติกผลิตภัณฑ์ของรายการปฏิทิน
ไฟล์ iCalendar บันทึกผลิตภัณฑ์ที่สร้างมันใน PRODID ฟิลด์. Aspose.Email รักษาตัวระบุนี้เมื่อนำรายการปฏิทินแปลงระหว่าง ICS และ MSG, เพื่อให้คุณสามารถติดตามแหล่งที่มาของรายการได้. ตัวระบุนี้เข้าถึงได้ผ่านคุณสมบัติดังต่อไปนี้:
product_idของ Appointment คลาส - รับหรือกำหนดตัวระบุของผลิตภัณฑ์ที่สร้างการนัดหมาย.product_idของ MapiCalendar คลาส - รับหรือกำหนดตัวระบุของผลิตภัณฑ์ที่สร้างรายการปฏิทิน.product_identifierของ MapiCalendarMsgSaveOptions และ MapiCalendarIcsSaveOptions คลาส - ตั้งค่าตัวระบุที่เขียนเมื่อบันทึกรายการปฏิทิน.
ตัวอย่างโค้ดนี้อ่านตัวระบุผลิตภัณฑ์จากไฟล์ ICS, แปลงนัดหมายเป็น MSG ด้วย AppointmentMsgSaveOptions, ทำการบันทึกรายการปฏิทินด้วยตัวระบุใหม่, แล้วเขียนตัวระบุอื่นไปยังไฟล์ ICS:
import io
import aspose.email as ae
from aspose.pycore import cast
# Read the product identifier from an iCalendar file
appointment = ae.calendar.Appointment.load("meeting.ics")
print("Appointment product ID:", appointment.product_id)
# The identifier is kept when the appointment is converted to MSG
msg_stream = io.BytesIO()
appointment.save(msg_stream, ae.calendar.AppointmentMsgSaveOptions())
msg_stream.seek(0)
msg = ae.mapi.MapiMessage.load(msg_stream)
if msg.supported_type == ae.mapi.MapiItemType.CALENDAR:
calendar = cast(ae.mapi.MapiCalendar, msg.to_mapi_message_item())
print("MapiCalendar product ID:", calendar.product_id)
# Save the calendar item to MSG with a different product identifier
options = ae.mapi.MapiCalendarMsgSaveOptions()
options.product_identifier = "-//Contoso//Calendar 2.0//EN"
calendar.save("meeting_out.msg", options)
# The identifier set on the item is written to the PRODID field of an ICS file
calendar.product_id = "-//Contoso//Calendar 3.0//EN"
calendar.save("meeting_out.ics", ae.mapi.MapiCalendarIcsSaveOptions())
นับจำนวนเหตุการณ์ปฏิทินทั้งหมด
นี้ CalendarReader คลาสนี้ให้ฟังก์ชันการอ่านและวิเคราะห์ข้อมูลปฏิทินจากไฟล์ที่ระบุ โดยการเริ่มต้นอ็อบเจ็กต์ CalendarReader คุณสามารถดึงรายละเอียดสำคัญ เช่น จำนวนเหตุการณ์ทั้งหมด, เวอร์ชันของปฏิทิน, วิธีการที่ใช้, และว่าปฏิทินมีหลายเหตุการณ์หรือไม่ ด้านล่างเป็นโค้ดตัวอย่างที่แสดงวิธีโต้ตอบกับข้อมูลปฏิทินและโหลดนัดหมายเป็นรายการเหตุการณ์ที่ครอบคลุม:
import aspose.email as ae
reader = ae.calendar.CalendarReader("calendar.ics")
print(f"Calendar contains {reader.count} events")
print(f"The Version of the calendar is {reader.version}")
print(f"The Method of the calendar is {reader.method}")
print(f"Does the calendar contain multiple events? - {reader.is_multi_events}")
appointments = reader.load_as_multiple()
เพิ่มการแจ้งเตือนแบบแสดงผลให้กับรายการปฏิทิน
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเพิ่มการแจ้งเตือนแบบแสดงผลไปยังรายการปฏิทิน:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.reminder_set = True
calendar.reminder_delta = 45 # 45 minutes before the start of the event
calendar.save(data_dir + "AddDisplayReminderToCalendar_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
calendar.save(data_dir + "AddDisplayReminderToCalendar_out.msg", ae.calendar.AppointmentSaveFormat.MSG)
เพิ่มการแจ้งเตือนแบบเสียงให้รายการปฏิทิน
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเพิ่มการแจ้งเตือนด้วยเสียงไปยังรายการปฏิทิน:
import aspose.email as ae
from datetime import datetime
# Create the appointment
calendar = ae.mapi.MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
datetime(2026, 6, 1, 21, 30, 0),
datetime(2026, 6, 1, 21, 50, 0))
calendar.reminder_set = True
calendar.reminder_delta = 45 # 45 minutes before the start of the event
calendar.reminder_file_parameter = "file://Alarm01.wav"
calendar.save(data_dir + "AddAudioReminderToCalendar_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
ตั้งค่าสถานะ MapiCalendar อย่างชัดเจน
Aspose.Email มีเมธอดเพื่อกำหนดสถานะของอ็อบเจ็กต์ MapiCalendar อย่างชัดเจน, แทนที่พฤติกรรมเริ่มต้น:
- void MapiCalendar.set_state_forced(MapiCalendarState state) - ให้การควบคุมที่มากขึ้นต่อสถานะของรายการปฏิทิน, โดยเฉพาะเมื่อทำงานกับคำขอประชุมที่ได้รับ.
เมื่อสร้างวัตถุ MapiCalendar มันจะได้รับการกำหนดสถานะ MapiCalendarState.MEETING โดยอัตโนมัติ เมื่อรายการถูกผู้รับรับ จะเปลี่ยนสถานะเป็น MapiCalendarState.RECEIVED และคลาสข้อความจะอัปเดตเป็น "IPM.Schedule.Meeting.Request"
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างนัดหมายประชุมโดยใช้ Aspose.Email สำหรับ Python ผ่าน .NET, ตั้งค่าผู้จัดและสถานะปฏิทินด้วยตนเอง, และบันทึกนัดหมายเป็นไฟล์ Outlook .msg:
from aspose.email.mapi import MapiCalendar, MapiCalendarState, MapiElectronicAddress
from aspose.email.calendar import AppointmentSaveFormat
from datetime import datetime, timezone
# Create a MapiCalendar appointment
appointment = MapiCalendar(
"LAKE ARGYLE WA 6743", # Location
"Appointment", # Subject
"This is a very important meeting :)", # Body
datetime(2024, 5, 10, 12, 30, 0, tzinfo=timezone.utc), # Start time (UTC)
datetime(2024, 5, 10, 13, 30, 0, tzinfo=timezone.utc) # End time (UTC)
)
# Set the organizer
appointment.organizer = MapiElectronicAddress()
appointment.organizer.email_address = "test@aaa.com"
appointment.organizer.display_name = "test display Name"
# Forcefully set the calendar state to Meeting | Received
appointment.set_state_forced(MapiCalendarState.MEETING | MapiCalendarState.RECEIVED)
# Save the appointment to a .msg file
appointment.save("appointment.msg", AppointmentSaveFormat.MSG)
โดยการเรียก set_state_forced, คุณสามารถตั้งค่าสถานะของรายการปฏิทินโดยกำหนดค่าเฉพาะ (เช่น MapiCalendarState.RECEIVED) ได้ด้วยตนเอง ซึ่งเป็นประโยชน์พิเศษเมื่อคุณต้องการ:
-
จำลองคำขอประชุมที่ได้รับ.
-
รักษาข้อมูลเมทาดาต้าของผู้จัดและการประชุมเมื่อตีบันทึกนัดหมายเป็นไฟล์ .MSG.
-
ปรับแต่งกระบวนการทำงานของปฏิทินเมื่อพฤติกรรมเริ่มต้นไม่เพียงพอ.
หมายเหตุ: การตั้งค่าสถานะด้วยตนเองด้วย
set_state_forcedอาจส่งผลต่อพฤติกรรมของ Outlook เช่น ความสามารถในการส่งต่อหรือส่งซ้ำการประชุม ดังนั้นควรใช้ด้วยความระมัดระวังและเฉพาะเมื่อจำเป็นสำหรับตรรกะของแอปพลิเคชันของคุณ.
การจัดการไฟล์ปฏิทิน
เพิ่มหรือดึงไฟล์แนบจากไฟล์ปฏิทิน
โมดูลปฏิทินของ Aspose.Email (aspose.email.calendar) ยังให้ฟังก์ชันการเพิ่มและดึงข้อมูลไฟล์แนบ.
เพื่อติดไฟล์แนบกับนัดหมาย, ทำตามขั้นตอนด้านล่าง:
- สร้าง Attachment อ็อบเจ็กต์ที่ให้เส้นทางของไฟล์.
- เพิ่มไฟล์แนบนี้ไปยังรายการไฟล์แนบของนัดหมาย.
- บันทึกนัดหมายลงในเส้นทางไฟล์ที่ระบุในรูปแบบ iCalendar โดยใช้ บันทึก เมธอดและรูปแบบไฟล์ที่เหมาะสม.
เพื่อดึงไฟล์แนบจากนัดหมาย, ทำตามขั้นตอนต่อไปนี้:
- โหลดนัดหมายจากไฟล์ที่บันทึกโดยใช้ โหลด เมธอด.
- ดึงจำนวนไฟล์แนบในนัดหมายโดยใช้คุณสมบัติ .attachments.length.
- วนซ้ำผ่านแต่ละไฟล์แนบ.
- พิมพ์ชื่อของแต่ละไฟล์แนบโดยใช้คุณสมบัติ .name ภายในลูป.
โค้ดตัวอย่างด้านล่างแสดงวิธีสร้างนัดหมายพร้อมไฟล์แนบ, บันทึกมัน, และดึงข้อมูลไฟล์แนบโดยใช้โมดูล "ae.calendar" ซึ่งเน้นฟังก์ชันและความสามารถของการทำงานกับนัดหมายและไฟล์แนบในแอปพลิเคชันปฏิทิน.
import aspose.email as ae
import datetime as dt
# Add an attachment to an appointment
attendees = ae.MailAddressCollection()
attendees.append(ae.MailAddress("attendee@domain.com", "Recipient 1"))
appointment = ae.calendar.Appointment("Room 112", dt.datetime(2023, 5, 27), dt.date(2023, 5, 28), ae.MailAddress("organizer@domain.com"), attendees)
attachment = ae.Attachment(data_dir + "Attachment.txt")
appointment.attachments.append(attachment)
appointment.save(data_dir + "appWithAttachments_out.ics", ae.calendar.AppointmentSaveFormat.ICS)
# Retrieve attachments from an appointment
appointment2 = ae.calendar.Appointment.load(data_dir + "appWithAttachments_out.ics")
print(len(appointment2.attachments))
for att in appointment2.attachments:
print(att.name)
ดึงสถานะผู้รับจากคำขอประชุม
Aspose.Email ให้คุณทำงานกับคำขอประชุมที่แสดงเป็นอ็อบเจ็กต์ MapiCalendar และดึงสถานะของผู้รับ (การติดตามการเข้าร่วม) จากคำขอประชุมดังกล่าว โค้ดตัวอย่างต่อไปนี้แสดงวิธีระบุสถานะของอีเมลสำหรับแต่ละผู้รับ:
from aspose.email.mapi import MapiMessage
message = MapiMessage.load(data_dir + "MeetingRequest.msg")
for recipient in message.recipients:
print(f"{recipient.display_name}: {recipient.recipient_track_status}")
แปลงนัดหมาย EML เป็น MSG พร้อมรักษา HTML Body
Aspose.Email ทำให้สามารถแปลงนัดหมาย EML เป็น MSG และรักษา HTML Body ได้ คุณสมบัติ forced_rtf_body_for_appointment ของ MapiConversionOptions คลาสนี้ใช้เพื่อจัดการประเภทของเนื้อหานัดหมาย เมื่อตั้งค่าเป็น True เนื้อหาจะถูกแปลงเป็นรูปแบบ RTF โดยค่าเริ่มต้น เพื่อรักษาเนื้อหาเป็นรูปแบบ HTML ควรตั้งค่าเป็น False ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรักษา HTML Body ของนัดหมายเมื่อแปลงจาก EML ไปเป็น MSG:
import aspose.email as ae
eml = ae.MailMessage.load("appointment.eml")
conversionOptions = ae.mapi.MapiConversionOptions()
conversionOptions.format = ae.mapi.OutlookMessageFormat.UNICODE
# default value for ForcedRtfBodyForAppointment is true
conversionOptions.forced_rtf_body_for_appointment = False
msg = ae.mapi.MapiMessage.from_mail_message(eml, conversionOptions)
print(f"Body Type: {msg.body_type}")
msg.save("appointment.msg")
ตั้งการเตือนความจำสำหรับการนัดหมาย
การเตือนสามารถเพิ่มได้เมื่อสร้างนัด การเตือนเหล่านี้อาจถูกกระตุ้นด้วยเงื่อนไขต่างๆ — n นาทีก่อนเริ่มกำหนด, ทำซ้ำ n ครั้งที่ช่วงเวลา n, เป็นต้น ในนัดของ ICS การกระตุ้นเหล่านี้อธิบายโดยแท็กที่อยู่ใน BEGIN:VALARM และ END:VALARM.
เพิ่มแท็กเพื่อตั้งการเตือนความจำ
Aspose.Email เปิดเผยบล็อกการเตือนผ่าน reminders คอลเลกชันของ Appointment class และ AppointmentReminder, ReminderTrigger, ReminderDuration, ReminderAttachment และ ReminderAttendee คลาส ประเภทของการเตือนกำหนดโดย ReminderAction enumeration — AUDIO, DISPLAY, EMAIL หรือ PROCEDURE.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตั้งการเตือนหลายรายการในนัดเดียว:
import datetime
import aspose.email as ae
from aspose.email.calendar import (Appointment, AppointmentReminder, ReminderTrigger, ReminderDuration,
ReminderAction, ReminderAttachment, ReminderAttendee, ReminderRelated)
location = "Meeting Location: Room 5"
start_date = datetime.datetime(1997, 3, 18, 18, 30, 0)
end_date = datetime.datetime(1997, 3, 18, 19, 30, 0)
organizer = ae.MailAddress("aaa@amail.com", "Organizer")
attendees = ae.MailAddressCollection()
attendees.append(ae.MailAddress("bbb@bmail.com", "First attendee"))
target = Appointment(location, start_date, end_date, organizer, attendees)
# Audio alarm that will sound at a precise time and repeat 4 more times at 15 minute intervals
audio_reminder = AppointmentReminder()
audio_reminder.trigger = ReminderTrigger(datetime.datetime(1997, 3, 17, 13, 30, 0))
audio_reminder.repeat = 4
audio_reminder.duration = ReminderDuration(datetime.timedelta(minutes=15))
audio_reminder.action = ReminderAction.AUDIO
audio_reminder.attachments.append(ReminderAttachment("ftp://Host.com/pub/sounds/bell-01.aud"))
target.reminders.append(audio_reminder)
# Display alarm that will trigger 30 minutes before the scheduled start of the event
# and will repeat 2 more times at 15 minute intervals
display_reminder = AppointmentReminder()
display_reminder.trigger = ReminderTrigger(ReminderDuration(datetime.timedelta(minutes=-30)), ReminderRelated.START)
display_reminder.repeat = 2
display_reminder.duration = ReminderDuration(datetime.timedelta(minutes=15))
display_reminder.action = ReminderAction.DISPLAY
display_reminder.description = "Breakfast meeting with executive team at 8:30 AM EST"
target.reminders.append(display_reminder)
# Email alarm that will trigger 2 days before the scheduled start. It does not repeat.
email_reminder = AppointmentReminder()
email_reminder.trigger = ReminderTrigger(ReminderDuration(datetime.timedelta(days=-2)), ReminderRelated.START)
email_reminder.attendees.append(ReminderAttendee("john_doe@host.com"))
email_reminder.action = ReminderAction.EMAIL
email_reminder.summary = "REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING"
email_reminder.description = "A draft agenda needs to be sent out to the attendees of the weekly managers meeting."
email_reminder.attachments.append(ReminderAttachment("http://Host.com/templates/agenda.doc"))
target.reminders.append(email_reminder)
target.save("AppointmentWithReminders_out.ics")
MapiCalendarTimeZone จากเขตเวลาระบบด้วย MapiCalendarTimeZone(TimeZoneInfo) คอนสตรักเตอร์ ใน Aspose.Email for Python via .NET MapiCalendarTimeZone คลาสอ่านอย่างเดียว — มันเปิดเผย key_name และ time_zone_rules ของเขตเวลาที่ได้เก็บไว้แล้วในรายการปฏิทินและไม่สามารถสร้างจากเขตเวลามาตรฐานได้
ดูเพิ่มเติม
- การจัดการการเกิดซ้ำ — สร้างรูปแบบการทำซ้อน MAPI รายวัน, รายสัปดาห์, รายเดือนและรายปี