Outlook Calendar Entries for a Day with Python

For me it is not the mobile. It is the email, which is the most distraction application in office.

The variable reward system has become a habit. I tried closing it but then I keep missing meetings etc.

So had to keep it open just so the reminders for meetings show up.

Only reason I need the outlook is to have a day view why not print it. Bad option, see the already printed stack on my desk. Why not get all the events out for a day and use a text file on the desktop.

Therefore, that is what I did.

Here’s the python function to get the appointments of the day from outlook calendar.

import win32com.client, datetime

def getCalendarEntries(days=1):
    """
    Returns calender entries for days default is 1
    """
    Outlook = win32com.client.Dispatch("Outlook.Application")
    ns = Outlook.GetNamespace("MAPI")
    appointments = ns.GetDefaultFolder(9).Items
    appointments.Sort("[Start]")
    appointments.IncludeRecurrences = "True"
    today = datetime.datetime.today()
    begin = today.date().strftime("%m/%d/%Y")
    tomorrow= datetime.timedelta(days=days)+today
    end = tomorrow.date().strftime("%m/%d/%Y")
    appointments = appointments.Restrict("[Start] >= '" +begin+ "' AND [END] <= '" +end+ "'")
    events={'Start':[],'Subject':[],'Duration':[]}
    for a in appointments:
        adate=datetime.datetime.fromtimestamp(int(a.Start))
        #print a.Start, a.Subject,a.Duration
        events['Start'].append(adate)
        events['Subject'].append(a.Subject)
        events['Duration'].append(a.Duration)
    return events

The functions spits out a dict and can be easily converted used in any way one wants.

Sweet! Let's see if am able to kill outlook now.

12 thoughts on “Outlook Calendar Entries for a Day with Python

  1. for me, %m/%d in strftime had to be reversed to %d/%m, and
    adate=datetime.datetime.fromtimestamp(int(a.Start))

    threw a type error

    otherwise works like a charm. thank you!

    Like

  2. Hi, I didn’t change the date format, and I get a type error with the adate in the For loop. This is the message i get :
    TypeError: int() argument must be a string, a bytes-like object or a number, not ‘pywintypes.datetime’
    Since I am new to python, I feel I am a bit lost on how to solve it. Do you have any idea?
    I managed to run the whole code with no errors, by commenting the adate lines.
    thanks in advance

    Like

  3. I also have the TypeError, could you help me with this?
    line 20, in getCalendarEntries
    adate=datetime.datetime.fromtimestamp(int(a.Start))
    TypeError: int() argument must be a string, a bytes-like object or a number, not ‘pywintypes.datetime’

    Like

  4. Pingback: Get Outlook Entries With Python 3 | SukhbinderSingh.com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s