gpt4 book ai didi

python - 通过 Python 读取 Outlook 事件

转载 作者:太空狗 更新时间:2023-10-29 16:55:17 24 4
gpt4 key购买 nike

Outlook 有一些需要的东西——比如显示多月 View

我决定尝试一下,通过 Python 提取事件数据(然后想办法很好地显示它)。 Google 给出的结果很差。

我的目标是:

  • 阅读共享日历
  • 阅读事件信息,如开始、结束、主题、创建者等。

这就是我收集的(灵感来自 this site)

import win32com.client, datetime
from dateutil.relativedelta import relativedelta

Outlook = win32com.client.Dispatch("Outlook.Application")
ns = Outlook.GetNamespace("MAPI")

appointments = namespace.GetDefaultFolder(9).Items
# TODO: Need to figure out howto get the shared calendar instead Default [9]
# (I have placed the shared folder into a separate folder - don't know if it matters)
# I would just like the user to select which calendar to execute on
appointments.Sort("[Start]")
appointments.IncludeRecurrences = "True"
begin = date.today().strftime("%m%d%Y")
end = (date.today() + relativedelta( months = 3 )).strftime("%m%d%Y")
appointments = appointments.Restrict("[Start] >= '" +begin+ "' AND [END] >= '" +end+ "'")

我如何遍历事件并读取它们?

最佳答案

From here I need help with looping through the events and read them.

基本上,您所要做的就是遵循 Microsoft 的 COM API 文档。例如,Restrict()方法返回 AppointmentItem AppointmentItem Object 中记录的对象对于 Outlook 2010。因此,从一个文件夹开始,您可以获取并列出约会,如下所示:

# Get the AppointmentItem objects
# http://msdn.microsoft.com/en-us/library/office/aa210899(v=office.11).aspx
appointments = someFolder.Items

# Restrict to items in the next 30 days (using Python 3.3 - might be slightly different for 2.7)
begin = datetime.date.today()
end = begin + datetime.timedelta(days = 30);
restriction = "[Start] >= '" + begin.strftime("%m/%d/%Y") + "' AND [End] <= '" +end.strftime("%m/%d/%Y") + "'"
restrictedItems = appointments.Restrict(restriction)

# Iterate through restricted AppointmentItems and print them
for appointmentItem in restrictedItems:
print("{0} Start: {1}, End: {2}, Organizer: {3}".format(
appointmentItem.Subject, appointmentItem.Start,
appointmentItem.End, appointmentItem.Organizer))

请注意,我必须为限制表达式使用稍微不同的时间格式("%m/%d/%Y" 而不是 "%m%d%Y")。正确的解决方案是使用 Outlook 的 Format功能如 http://msdn.microsoft.com/en-us/library/office/ff869597(v=office.14).aspx 中所述, 日期 部分。另请注意,我使用的是 Python 3.3,因此您可能必须使用不同的函数来创建日期。在任何情况下,出于测试目的,您都可以使用硬编码表达式,如 "[Start] >= '02/03/2014' AND [End] <= '03/05/2014'"

要获取共享日历,以下代码应该可以工作——这是在 API 文档中找到的常见顺序,但是我无法真正让它工作,但这可能是由于不同的后端服务器(未使用Exchange 服务器):

recipient = namespace.createRecipient("User Name")
resolved = recipient.Resolve()
sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9)

要将所有可用文件夹显示为树状结构,您可以使用类似

def folderTree(folders, indent = 0):
prefix = ' ' * (indent*2)
i = 0
for folder in folders:
print("{0}{1}. {2} ({3})".format(prefix, i, folder.Name, folder.DefaultItemType))
folderTree(folder.Folders, indent + 1)
i = i + 1

...
folderTree(namespace.Folders)

要按路径查找文件夹(例如,在“Internet Calendars”文件夹下查找日历文件夹“Norfeld@so.com”),您可以使用类似

def findFolder(folders, searchPath, level = 0):
for folder in folders:
if folder.Name == searchPath[level]:
if level < len(searchPath)-1:
# Search sub folder
folder = findFolder(folder.folders, searchPath, level+1)
return folder
return None

...
sharedCalendar = findFolder(namespace.Folders, ["Internet Calendars", "Norfeld@so.com"])

另见:

关于python - 通过 Python 读取 Outlook 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477599/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com