【问题标题】:Problem with read Outlook shared calendar via python通过 python 读取 Outlook 共享日历的问题
【发布时间】:2019-11-21 09:51:43
【问题描述】:

我尝试制作一个应用程序来读取 Outlook 共享日历中的事件。我在 win10 上使用 Python 3.8.0。这是我的功能。

 def getSharedCalendarEntries(TS_name, days=1000): #TS_name is name of shared calendar
        MailboxToAccess = 'owner@gmail.com'
        Outlook = win32com.client.Dispatch("Outlook.Application")
        namespace = Outlook.GetNamespace("MAPI")
        recipient = namespace.createRecipient(MailboxToAccess)
        resolved = recipient.Resolve()
        sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9).Folders(TS_name).Items 
        sharedCalendar.Sort("[Start]")
        sharedCalendar.IncludeRecurrences = 'True'
        today = datetime.datetime(2019,1,1)
        begin = today.date().strftime('%d/%m/%Y')
        tomorrow = datetime.timedelta(days=days)+today
        end = tomorrow.date().strftime('%d/%m/%Y')
        sharedCalendar = sharedCalendar.Restrict("[Start] >= '" +begin+ "' AND [END] <= '" +end+ "'")
        events = {'Start':[],'End':[],'Subject':[],'Duration':[]}
        mEv = []
        for app in sharedCalendar: #petla po rezerwacjach
            adate = datetime.datetime(app.Start.year, app.Start.month, app.Start.day).date()
            events['Start'].append(adate)
            aend = datetime.datetime(app.End.year, app.End.month, app.End.day).date()
            events['End'].append(aend)
            events['Duration'].append(int(app.Duration/1440))
            events['Subject'].append(app.Subject)
            mEvent = Event(adate, aend, int(app.Duration/1440), app.Subject)
            mEv.append(mEvent)
        return mEv

一切正常,我能够读取事件,但突然发生了一些事情(我没有更改代码中的任何内容)并且出现这样的错误:

文件“C:\Users\user_catalog\Desktop\outread.py”,第 60 行,在 获取共享日历条目 sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9).Folders(TS_name).Items

文件 "C:\Users\user_catalog\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", 第 197 行,在 调用 return self._get_good_object_(self.oleobj.Invoke(*allArgs),self.olerepr.defaultDispatchName,None) pywintypes.com_error: (-2147352567, '发生异常。', (4096, 'Microsoft Outlook', ' 尝试执行操作失败。 找不到对象。', None, 0, -2147221233), None)

我拥有共享日历的只读访问权限。共享日历的所有者说她注销了网络,注销的时间与我的应用程序停止工作的时间相同。

你们有没有遇到过这样的问题或有什么建议给我? 提前谢谢!

皮奥

【问题讨论】:

    标签: python outlook outlook-calendar


    【解决方案1】:

    您介意试试下面的代码吗?它将为您提供一个包含主题、会议发生、开始时间和结束时间的数据框。

    import win32com.client, datetime
    import pandas as pd
    from datetime import time, timedelta
    
    #connect to outlook
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    appointments  = outlook.GetDefaultFolder(9).Items #Calendário
    
    #use these 2 lines to short the list
    appointments.Sort('[Start]')
    
    #Recurrent events  only show the first start date, use the following to get the REAL occurrence of the event.
    appointments.IncludeRecurrences = 'True'
    
    #Beginning of the week(monday) to end of week(friday)
    today = datetime.date.today()
    start = today - timedelta(days=today.weekday())
    end = start + timedelta(days=5)
    
    #String of restriction time that will be used to filter dates on outlook
    restriction = "[Start] >= '" + start.strftime("%d/%m/%Y") + "' AND [End] < '" +end.strftime("%d/%m/%Y") + "'"
    print(restriction)
    restrictedItems = appointments.Restrict(restriction)
    
    
    #create empty data frame with columns to be fetched
    i = 0
    calendario = pd.DataFrame(columns=['Subject', 'When', 'Start Time','End Time'])
    
    
    #Loop on items to fill the dataframe
    for appointmentItem in restrictedItems:
        
        calendario.loc[i] = [appointmentItem.Subject,appointmentItem.Start.strftime("%m/%d/%Y"),appointmentItem.Start.strftime("%H:%M"),appointmentItem.End.strftime("%H:%M")] 
        i = i + 1
    
    #display dataframe   
    calendario
    
    

    【讨论】:

      猜你喜欢
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多