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 **.
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.
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("----")
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