download recurring appointments but all appointments start date show 1st same date

45 Views Asked by At

I am trying to get all appointments from client outlook with python and win32com. client, there are several recurring appointments in calendar A. however, when I download these recurring appointments through python, it only shows the 1st appointment start date instead of each different dates. IncludeRecurrences = True was used in my coding; how can i modify my python coding? thanks

Example: a recurring appointment happens on the first of every month in 2024. I want to show 12 INSTANCES, with 12 DIFFERENT start dates (01/01/2024, 02/01/2024, etc) but currently only shows 01/01/2024 and repeat 12 times.

    import win32com.client, datetime
    import time
    import os
    import email
    import imaplib
    import pandas as pd
    import xlsxwriter
    import datetime as dt
    import calendar as ccc
    from datetime import timedelta
    
    MailboxToAccess = "[email protected]"
    outlook = win32com.client.Dispatch('Outlook.Application').GetNamespace('MAPI')
    recip = outlook.CreateRecipient(MailboxToAccess)    
    mail_box = outlook.Folders["A_mailbox"] 
    calendar = mail_box.Folders["Calendar"].Items
    
    
    calendar.IncludeRecurrences = True
    calendar.Sort('[Start]')
    _today=datetime.date.today()[enter image description here](https://i.stack.imgur.com/kdnV3.png)
    first_day = datetime.date(_today.year, _today.month, 1)
    this_month_end = datetime.datetime(_today.year, _today.month, ccc.monthrange(_today.year, _today.month)[1])
     
     
    restriction = "[Start] >= '" + str(first_day) + "' AND [END] <= '" + str(this_month_end) + "'"
    
    print (restriction)
    calendar = calendar.Restrict(restriction)
1

There are 1 best solutions below

1
Eugene Astafiev On

First, if you deal with a shared calendar in Outlook, you need to use the GetSharedDefaultFolder method which is used in a delegation scenario, where one user has delegated access to another user for one or more of their default folders (for example, their shared Calendar folder). In that case you will not have to build the codebase on string literals:

mail_box = outlook.Folders["A_mailbox"] 
calendar = mail_box.Folders["Calendar"]

Also pay attention to the following code which re-assigns the calendar object:

calendar = calendar.Restrict(restriction)

The Restrict method returns a new Items instance with the result of filtering. It makes sense to separate two objects to distinguish them later.

Finally, in the code sample it is not clear how the result collection is used further. You may find the article which I wrote for the technical blog more than a decade ago helpful, see How To: Use Restrict method in Outlook to get calendar items.