Python调用Outlook发送会议邀请如何避免显示在本人日历
解决方案
方案1:添加单参数实现发送后自动删除本地日历项(仅需修改1行代码,零额外依赖)
Outlook 原生提供DeleteAfterSubmit属性,设置为True后,会议邀请发送成功会自动从组织者的日历中删除,不会产生残留,也不影响目标用户接收邀请。
修改后的代码如下:
import win32com.client outlook = win32com.client.Dispatch("Outlook.Application") def sendMeeting(): appt = outlook.CreateItem(1) # AppointmentItem appt.Start = "2021-12-09 10:10" # yyyy-MM-dd hh:mm appt.Subject = "Test test" appt.Duration = 60 appt.Location = "Out of office" appt.MeetingStatus = 1 # 1 - olMeeting; Changing the appointment to meeting. Only after changing the meeting status recipients can be added appt.Organizer = "user@email.com" # 新增以下两个参数 appt.DeleteAfterSubmit = True # 发送后自动删除本地日历项 appt.ResponseRequested = False # 不需要用户回复接受/拒绝,减少用户干扰 appt.Recipients.Add("user@email.com") # Don't end ; as delimiter appt.Save() appt.Send() print("sent") sendMeeting()
方案2:直接写入目标用户日历(无邀请邮件,完全不触碰本地日历)
如果你的企业Exchange账号已被IT管理员授予目标用户的日历编辑权限(无需收集用户账号密码,仅需管理员后台配置权限),可以直接在目标用户的日历中创建日程,全程不会在你的本地日历生成任何内容,也不会给用户发送邀请邮件,日程会直接展示在用户日历中。
示例代码如下:
import win32com.client outlook = win32com.client.Dispatch("Outlook.Application") namespace = outlook.GetNamespace("MAPI") def add_leave_to_user_calendar(user_email, start_time, subject, duration): # 获取目标用户的日历文件夹 recipient = namespace.CreateRecipient(user_email) recipient.Resolve() if recipient.Resolved: user_calendar = namespace.GetSharedDefaultFolder(recipient, 9) # 9 对应日历文件夹 # 创建日程 appt = user_calendar.Items.Add() appt.Start = start_time appt.Subject = subject appt.Duration = duration appt.Location = "Out of office" appt.Save() # 直接保存到用户日历,不需要发送 print(f"已为{user_email}添加日程") # 调用示例 add_leave_to_user_calendar("user@email.com", "2021-12-09 10:10", "假期", 60)
注意事项
- 方案1建议先做1-2个小批量测试,确认目标用户能正常收到日程、本地日历无残留后再执行全量批量任务
- 如果假期是全天事件,可以添加
appt.AllDayEvent = True参数,不需要设置具体时分
内容的提问来源于stack exchange,提问作者SomeGuy
相关产品推荐
相关产品推荐

