Quản lý mục Lịch Outlook
Cái MapiCalendar lớp trong thư viện Aspose.Email cung cấp nhiều phương thức và thuộc tính để cấu hình và thao tác các mục lịch. Điều này cho phép quản lý hiệu quả các mục lịch Outlook, nâng cao năng suất và tổ chức. Dưới đây là các phần sẽ hướng dẫn bạn qua các khía cạnh khác nhau của việc quản lý mục lịch.
Tạo và Lưu các mục Lịch
Tạo và Lưu mục Lịch
Đoạn mã dưới đây minh họa cách tạo và lưu một mục lịch ở định dạng ICS:
- Xác định đường dẫn nơi tệp lịch của bạn sẽ được lưu.
- Tạo một cuộc hẹn bằng cách khởi tạo một MapiCalendar đối tượng với các chi tiết cụ thể như địa điểm, tiêu đề, mô tả, thời gian bắt đầu và kết thúc.
- Lưu cuộc hẹn đã tạo dưới dạng tệp ICS trong thư mục đã chỉ định.
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)
Lưu mục Lịch thành MSG
Đoạn mã dưới đây cho bạn thấy cách lưu mục lịch thành 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)
Đặt ID Sản phẩm cho Chuyển đổi MapiCalendar sang ICS
Thư viện Aspose.Email cho phép bạn lưu một MapiCalendar (một mục lịch) thành tệp ICS (iCalendar) với các tùy chọn cụ thể. Dưới đây thuộc tính sẽ cho phép bạn giữ nguyên dấu thời gian ngày và giờ gắn với mục lịch và đặt mã định danh sản phẩm trong tệp ICS, ví dụ là "Foo Ltd", tương ứng:
- ics_save_options.keep_original_date_time_stamp
- ics_save_options.product_identifier
Mã định danh sản phẩm là trường xác định phần mềm hoặc dịch vụ đã tạo ra tệp ICS.
Dưới đây là mẫu mã cho phép bạn đặt ID sản phẩm khi lưu MapiCalendar thành 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)
Đọc và Đặt Định Danh Sản Phẩm của Mục Lịch
Một tệp iCalendar ghi lại sản phẩm tạo ra nó trong PRODID trường. Aspose.Email giữ lại định danh này khi một mục lịch được chuyển đổi giữa ICS và MSG, vì vậy bạn có thể truy nguồn gốc của mục. Định danh này có sẵn qua các thuộc tính sau:
product_idcủa Appointment lớp - lấy hoặc đặt định danh của sản phẩm đã tạo cuộc hẹn.product_idcủa MapiCalendar lớp - lấy hoặc đặt định danh của sản phẩm đã tạo mục lịch.product_identifiercủa MapiCalendarMsgSaveOptions và MapiCalendarIcsSaveOptions các lớp - đặt định danh được ghi khi mục lịch được lưu.
Đoạn mã mẫu sau đọc định danh sản phẩm từ tệp ICS, chuyển đổi cuộc hẹn sang MSG với AppointmentMsgSaveOptions, lưu mục lịch với định danh mới, và sau đó ghi định danh khác vào tệp 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())
Đếm Tổng số Sự kiện Lịch
Cái CalendarReader lớp cung cấp chức năng đọc và phân tích dữ liệu lịch từ một tệp được chỉ định. Bằng cách khởi tạo một đối tượng CalendarReader, bạn có thể trích xuất các chi tiết quan trọng như tổng số sự kiện, phiên bản lịch, phương pháp sử dụng và liệu lịch có bao gồm nhiều sự kiện hay không. Dưới đây là đoạn mã minh họa cách tương tác với dữ liệu lịch và tải các cuộc hẹn thành danh sách sự kiện đầy đủ:
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()
Thêm Lời Nhắc Hiển thị vào Mục Lịch
Đoạn mã sau cho bạn thấy cách thêm lời nhắc hiển thị vào một mục lịch:
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)
Thêm Lời Nhắc Âm thanh vào Mục Lịch
Đoạn mã sau cho bạn thấy cách thêm lời nhắc âm thanh vào một mục lịch:
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)
Đặt Trạng thái MapiCalendar một cách Rõ ràng
Aspose.Email cung cấp một phương pháp để xác định rõ ràng trạng thái của một đối tượng MapiCalendar, ghi đè hành vi mặc định:
- void MapiCalendar.set_state_forced(MapiCalendarState state) - cung cấp kiểm soát tốt hơn đối với trạng thái của mục lịch, đặc biệt khi làm việc với yêu cầu họp đã nhận.
Khi một đối tượng MapiCalendar được tạo, nó tự động được gán trạng thái MapiCalendarState.MEETING. Khi mục được người nhận nhận, trạng thái thường thay đổi thành MapiCalendarState.RECEIVED, và lớp tin nhắn được cập nhật thành "IPM.Schedule.Meeting.Request".
Mẫu mã dưới đây minh họa cách tạo một cuộc hẹn họp bằng Aspose.Email cho Python qua .NET, thiết lập người tổ chức và trạng thái lịch một cách thủ công, và lưu cuộc hẹn thành tệp 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)
Bằng cách gọi set_state_forced, bạn có thể đặt thủ công trạng thái của mục lịch thành giá trị cụ thể (ví dụ, MapiCalendarState.RECEIVED). Điều này đặc biệt hữu ích khi bạn muốn:
-
Mô phỏng một yêu cầu họp đã nhận.
-
Bảo toàn dữ liệu người tổ chức và thông tin họp khi lưu cuộc hẹn thành tệp .MSG.
-
Tùy chỉnh quy trình làm việc lịch khi hành vi mặc định không đủ.
Lưu ý: Thiết lập trạng thái thủ công bằng
set_state_forcedcó thể ảnh hưởng tới hành vi của Outlook, chẳng hạn khả năng chuyển tiếp hoặc gửi lại cuộc họp. Do đó, nên sử dụng một cách thận trọng và chỉ khi cần thiết cho logic của ứng dụng.
Quản lý Tệp Lịch
Thêm hoặc Lấy Tệp đính kèm từ Tệp Lịch
Mô-đun lịch của Aspose.Email (aspose.email.calendar) cũng cung cấp chức năng thêm và lấy thông tin tệp đính kèm.
Để bao gồm tệp đính kèm trong cuộc hẹn, thực hiện các bước dưới đây:
- Tạo một Attachment đối tượng cung cấp đường dẫn tệp.
- Thêm tệp đính kèm này vào danh sách tệp đính kèm của cuộc hẹn.
- Lưu cuộc hẹn vào đường dẫn tệp cụ thể ở định dạng iCalendar bằng lưu phương thức và định dạng tệp phù hợp.
Để lấy tệp đính kèm từ một cuộc hẹn, thực hiện các bước sau:
- Tải cuộc hẹn từ tệp đã lưu bằng load phương thức.
- Lấy số lượng tệp đính kèm trong cuộc hẹn bằng thuộc tính .attachments.length.
- Duyệt qua mỗi tệp đính kèm.
- In ra tên của mỗi tệp đính kèm bằng thuộc tính .name trong vòng lặp.
Đoạn mã dưới đây minh họa cách tạo cuộc hẹn có tệp đính kèm, lưu chúng và lấy thông tin tệp đính kèm bằng mô-đun "ae.calendar". Nó nêu bật chức năng và khả năng làm việc với cuộc hẹn và tệp đính kèm trong ứng dụng lịch.
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)
Lấy Trạng thái Người nhận từ Yêu cầu Họp
Aspose.Email cho phép bạn làm việc với yêu cầu họp, được biểu thị như một đối tượng MapiCalendar, và lấy trạng thái người nhận (theo dõi tham dự) từ các yêu cầu họp đó. Đoạn mã dưới đây cho bạn thấy cách xác định trạng thái email cho mỗi người nhận:
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}")
Chuyển đổi Cuộc Hẹn EML sang MSG đồng thời Bảo tồn Nội dung HTML
Aspose.Email cho phép chuyển đổi cuộc hẹn EML sang MSG và bảo tồn nội dung HTML. Thuộc tính forced_rtf_body_for_appointment của MapiConversionOptions lớp được sử dụng để thao tác loại nội dung của cuộc hẹn. Khi đặt là True, nội dung sẽ được chuyển sang định dạng RTF theo mặc định. Để giữ nội dung ở định dạng HTML, nên đặt là False. Đoạn mã dưới đây cho thấy cách bảo tồn nội dung HTML của cuộc hẹn khi chuyển đổi từ EML sang 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")
Đặt Nhắc Nhở cho Cuộc Hẹn
Một lời nhắc có thể được thêm khi tạo một cuộc hẹn. Những cảnh báo này có thể được kích hoạt bởi các tiêu chí khác nhau — n phút trước khi lịch bắt đầu, lặp lại n lần mỗi n khoảng thời gian, v.v. Trong một cuộc hẹn ICS, các kích hoạt này được mô tả bằng các thẻ được bao quanh trong BEGIN:VALARM và END:VALARM.
Thêm Thẻ để Đặt Nhắc Nhở
Aspose.Email mở rộng các khối cảnh báo thông qua reminders bộ sưu tập của Appointment class và AppointmentReminder, ReminderTrigger, ReminderDuration, ReminderAttachment và ReminderAttendee các lớp. Loại cảnh báo được xác định bởi ReminderAction enumeration — AUDIO, DISPLAY, EMAIL hoặc PROCEDURE.
Đoạn mã sau cho bạn thấy cách đặt nhiều lời nhắc trên một cuộc hẹn duy nhất:
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 từ một múi giờ hệ thống với MapiCalendarTimeZone(TimeZoneInfo) bộ khởi tạo. Trong Aspose.Email cho Python thông qua .NET, MapiCalendarTimeZone lớp chỉ đọc — nó mở rộng key_name và time_zone_rules của một múi giờ đã được lưu trong mục lịch, và không thể được xây dựng từ một múi giờ chuẩn.
Xem Thêm
- Quản lý Định kỳ — xây dựng các mẫu chu kỳ MAPI hàng ngày, hàng tuần, hàng tháng và hàng năm.