[Automation] Extract Outlook appointments with Python

I manage my schedule and achievements in Outlook, but ...

Since my company has an email culture, I use Outlook to exchange emails and manage work and meeting appointments in Outlook. We also keep track of when, what, and how much we have done in our Outlook schedule for daily and weekly performance reports.

As a result, ** Outlook calendar is full of appointments and achievement records, which makes it difficult to manage visually **.

If you want to write the business performance of the day in the weekly report, you also have to copy the Outlook calendar. My work is quite multitasking and the number of schedules per day is amazing, so it is very troublesome to manually check and copy.

If you use Python, you can extract the calendar from Outlook all at once, so you can format it as a text file or Excel and paste it as it is in a daily report.

In this article, I will introduce ** how to extract appointments with Python from Microsoft Outlook client software **.

Extract Outlook calendar with Python

You need to import win32com.client to operate outlook. I'm using Anaconda and I was able to import it without any additional installation.

python


import win32com.client

Then create an Outlook object and retrieve it with your calendar.

python


outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
calender = outlook.GetDefaultFolder(9) #"9" is the Outlook calendar

This calender is the calendar. Now, let's specify the period and extract the schedule. Here, I will extract the schedule of [2020-10-19 to 2020-10-23].

python


import datetime

items = calender.Items #This item is each "plan"

select_items = [] #A list of appointments within a specified period

#Specify the period for which you want to extract the schedule
start_date = datetime.date(2020, 10, 19) # 2020-10-19
end_date = datetime.date(2020, 10, 23) # 2020-10-23

for item in items:
    if start_date <= item.start.date() <= end_date:
        select_items.append(item)

#Display the details of the extracted schedule
for select_item in select_items:
    print("subject:", select_item.subject)
    print("place:", select_item.location)
    print("Start time:", select_item.start)
    print("End time:", select_item.end)
    print("Text:", select_item.body)
    print("----")

Execution result


Subject: Bug investigation
Location: Laboratory 1
Start time: 2020-10-20 10:00:00+00:00
End time: 2020-10-20 11:30:00+00:00
Body: Investigate the problem of hanging with Mr. A.
----
Subject: Code Review
Location: Meeting Room 3
Start time: 2020-10-22 14:00:00+00:00
End time: 2020-10-22 15:00:00+00:00
Body: Code review of Project 1234 with Mr. B and Mr. C.
----
​

You have extracted the schedule! If you devise a print statement according to the format such as daily report, you should be able to copy and paste the output result as it is.

Code summary

Finally, I will put the code together.

python


import win32com.client
import datetime

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

calender = outlook.GetDefaultFolder(9)

items = calender.Items #This item is each "plan"

select_items = [] #A list of appointments within a specified period

#Specify the period for which you want to extract the schedule
start_date = datetime.date(2020, 10, 19) # 2020-10-19
end_date = datetime.date(2020, 10, 23) # 2020-10-23

for item in items:
    if start_date <= item.start.date() <= end_date:
        select_items.append(item)

#Display the details of the extracted schedule
for select_item in select_items:
    print("subject:", select_item.subject)
    print("place:", select_item.location)
    print("Start time:", select_item.start)
    print("End time:", select_item.end)
    print("Text:", select_item.body)
    print("----")

Microsoft Office related automation

Click here for other automation series related to Microsoft Office. If you are interested, please!

[Automation] Send Outlook email with Python https://qiita.com/konitech913/items/51867dbe24a2a4272bb6

[Automation] Read Outlook emails with Python https://qiita.com/konitech913/items/8a285522b0c118d5f905

[Automation] Read mail (msg file) with Python https://qiita.com/konitech913/items/fa0cf66aad27d16258c0

[Automation] Read a Word document with Python https://qiita.com/konitech913/items/c30236bdf47775535e2f

Recommended Posts

[Automation] Extract Outlook appointments with Python
[Automation] Send Outlook email with Python
[Automation] Extract the table in PDF with Python
Extract the xz file with python
[Automation with python! ] Part 1: Setting file
[Beginner] Extract character strings with Python
[Automation with python! ] Part 2: File operation
[Python] Send an email with outlook
[Automation] Manipulate mouse and keyboard with Python
[Automation] Read a Word document with Python
[Automation] Read mail (msg file) with Python
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Extract text from PowerPoint with Python! (Compatible with tables)
GUI automation with Python x Windows App Driver
Extract zip with Python (Japanese file name support)
Extract email attachments received in Thunderbird with Python
Extract data from a web page with Python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Extract "current date only" and "current date and time" with python datetime.
Scraping with Python (preparation)
Try scraping with Python.
Extract the table of image files with OneDrive & Python
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
UI Automation in Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python