{"id":631,"date":"2004-09-17T21:20:16","date_gmt":"2004-09-18T01:20:16","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=631"},"modified":"2004-09-17T21:20:16","modified_gmt":"2004-09-18T01:20:16","slug":"sending-your-outlook-calendar-using-python","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/09\/17\/sending-your-outlook-calendar-using-python\/","title":{"rendered":"Sending your Outlook calendar using Python"},"content":{"rendered":"<p>At work we&#8217;re required to send an email with our personal schedules at the beginning of the month to everyone in our group so that (at least until your schedule changes) everyone knows where everyone else is.  Sure, we could be using a online calendar or some kind of group collaboration software but we&#8217;re stuck using <a href=\"http:\/\/www-306.ibm.com\/software\/lotus\/\">Lotus Notes<\/a>, which is the worst email client I&#8217;ve ever used (<a href=\"http:\/\/digilander.libero.it\/chiediloapippo\/Engineering\/iarchitect\/lotus.htm\">don&#8217;t believe me?<\/a>).  I switched to MS Outlook because a) I&#8217;ve used it before and b) the <a href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?FamilyId=8EBBBA59-5F17-4E52-8980-C4F0DFA92D65&amp;displaylang=en\">Notes Connector<\/a> gives me access to the Notes server.  Anyway, I&#8217;ve been wanting to tinker with Python for the last couple months and this seemed like the perfect project for it.  Using the excellent <a href=\"http:\/\/www.python.org\/windows\/win32com\/\">win32com<\/a> package, I was able to get a collection of all the items in my Outlook calendar, filter those events for the current month, put a message together with the subject, start date\/end date and start time\/end time and then send an email to an Outlook distribution list. <\/p>\n<p>Since I&#8217;m new to Python, I&#8217;m going to walk through the code, narrating what I *think* is happening as I go along. A link to the finished product is available at the end, please let me know what, if anything, I&#8217;m doing wrong&#8230;<\/p>\n<p>First things first, I import the appropriate modules (or packages?):<br \/>\n<code><br \/>\nimport win32com.client<br \/>\nimport time<br \/>\nimport datetime<br \/>\n<\/code><br \/>\nAfter that I dive headfirst into Outlook getting an Outlook Application object, log on to the MAPI namespace, and retrieve all the items in the Outlook Calendar.  Most examples I saw using &#8216;olCalendar&#8217; as the argument to the GetDefaultFolder() method, but got the impression that &#8216;olCalendar&#8217; is some sort of intrinsic constant that I didn&#8217;t have access too. This page that listed <a href=\"http:\/\/www.winscripter.com\/WSH\/MSOffice\/93.aspx\">all the Outlook constants<\/a> was where I got the number nine.<br \/>\n<code><br \/>\noutlook = win32com.client.Dispatch(\"Outlook.Application\")<br \/>\nnamespace = outlook.GetNamespace(\"MAPI\")<br \/>\nappointments = namespace.GetDefaultFolder(9).Items<br \/>\n<\/code><br \/>\nNext I sort the events by Start Date (right?) and flip the &#8216;include recurring events&#8217; flag to true:<br \/>\n<code><br \/>\nappointments.Sort(\"[Start]\")<br \/>\nappointments.IncludeRecurrences = \"True\"<br \/>\n<\/code><br \/>\nAfter that I do a bit of date munging to get the starting month\/year and the last day of the month\/year, use the Restrict() method to filter out only events in the current month and sort the events again:<br \/>\n<code><br \/>\n# start date<br \/>\nmonthNow = now.month<br \/>\nyearNow = now.year<br \/>\nbegin = datetime.date(now.year, now.month, 1)<br \/>\n# end date<br \/>\nnextMonth = begin + datetime.timedelta(days=35)<br \/>\nend = datetime.date(nextMonth.year, nextMonth.month, 1)<br \/>\nappointments = appointments.Restrict(<br \/>\n&nbsp;&nbsp;\"[Start] &gt;= '\" + begin.strftime(\"%m\/%d\/%Y\") +<br \/>\n&nbsp;&nbsp;\"' AND [End]<br \/>\nNow I'm left with a filtered and sorted collection of Outlook calendar items so the next step is to loop over the results, adding the subject, start time and end time to a message string:<br \/>\n<code><br \/>\nappointment = appointments.GetFirst()<br \/>\nmessage = begin.strftime(\"%B, %Y\") + \" Meetings &amp; Events for Aaron Johnson\\n\"<br \/>\nmessage += \"<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work we&#8217;re required to send an email with our personal schedules at the beginning of the month to everyone in our group so that (at least until your schedule changes) everyone knows where everyone else is. Sure, we could be using a online calendar or some kind of group collaboration software but we&#8217;re stuck &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/09\/17\/sending-your-outlook-calendar-using-python\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Sending your Outlook calendar using Python<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[25],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/631"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}