通过Python读取Outlook事件

Nor*_*ldt 16 python win32com outlook-2010 python-2.7

Outlook有一些需要的东西 - 比如显示多个月的视图

所以我决定尝试通过python拉出事件数据(然后找到一种方法来很好地显示它).谷歌正在给我毛孔结果,但stackoverflow以前在使用win32com和outlook方面非常有帮助.

我的目标如下

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

我没有走远,但这就是我聚在一起的(来自这个网站的灵感)

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+ "'")
Run Code Online (Sandbox Code Playgroud)

从这里我需要帮助循环事件并阅读它们.任何帮助都非常感谢.

And*_*ter 20

从这里我需要帮助循环事件并阅读它们.

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

# 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))
Run Code Online (Sandbox Code Playgroud)

请注意,我必须使用稍微不同的时间格式来表示限制表达式("%m/%d/%Y"而不是"%m%d%Y").正确的解决方案是使用Outlook的Format功能,如http://msdn.microsoft.com/en-us/library/office/ff869597(v=office.14).aspx,section Date部分所述.另请注意,我使用的是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)
Run Code Online (Sandbox Code Playgroud)

要将所有可用文件夹显示为树,您可以使用类似的内容

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)
Run Code Online (Sandbox Code Playgroud)

要按其路径查找文件夹(例如,要在"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"])
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • 我同意 COM API 不容易阅读 - 但基本上,该模型很简单:有些对象具有方法和属性(以及事件)。可以调用方法并可能返回结果,例如对另一个对象的引用。属性可以读取(如果不是只读,也可以写入),并且读取可能会再次导致对另一个对象的引用。一些方法/属性返回可迭代的 COM 集合,它无缝集成到 Python 中,以便您可以使用“for ... in ...”。 (2认同)