Reputation: 11
I try to make an application to read events from Outlook shared calendar. I use Python 3.8.0 on win10. Here is my function to do this.
def getSharedCalendarEntries(TS_name, days=1000): #TS_name is name of shared calendar
MailboxToAccess = '[email protected]'
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
Everything was working and I was able to read events, but suddenly something happened (I didn't change anything in code) and I have such error:
File "C:\Users\user_catalog\Desktop\outread.py", line 60, in getSharedCalendarEntries sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9).Folders(TS_name).Items
File "C:\Users\user_catalog\AppData\Local\Programs\Python\Python38\lib\site-packages\win32com\client\dynamic.py", line 197, in call return self._get_good_object_(self.oleobj.Invoke(*allArgs),self.olerepr.defaultDispatchName,None) pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', ' An attempt to perform the operation failed. Could not find the object.', None, 0, -2147221233), None)
I had read-only access to shared calendars. Owner of shared calendars said that she logged-out of network, and time of logged out was the same time my application stopped working.
Have any of you had such problem or have some tips for me? Thank you in advance!
Pio
Upvotes: 1
Views: 1518
Reputation: 141
Would you mind trying the code below? It will get you a dataframe with subject, occurence of the meeting, start time and end time.
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
Upvotes: 0