Member-only story
PythonでOutlook の使い方を分析する
とあるプロジェクトの振り返りであまりにもメール多すぎというかコミュニケーションの難しさを感じたのでOutlookの受信箱にあるそのプロジェクトに関係したメールがどれくらいあってどの程度時間をかけたのかを知るために色々やってみた備忘録
まずPythonでOutlookのメールを取得するためにはWin32.comのモジュールを使う。
Outlookの定義はドキュメント通りにして、通常のInboxはOutlookの中ではフォルダ6番として定義されているらしいのでデフォルトフォルダを6に。このへんは自分で作った下位フォルダを定義する番号が分からなかったので当該メールをすべて受信箱に移動してから作業している(いい方法あったら誰か教えてください)
import win32com.client
import os
import pandas as pd
import numpy as np
#read outlook items
outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
inbox = outlook.GetDefaultFolder(6)
message = inbox.Items
message2 = message.GetLast()
subject = message2.Subject
body = message2.body
date = message2.senton.date()
sender = message2.Sender
attachments = message2.Attachments
タイトルと本文、日付(これは送信日と受信日どっちでも定義できるみたいだけど今回は送信日)、送信者と一応添付も定義した。
添付と本文は今回の分析では使わないのでタイトルと送信者と日付が定義できていればOK
#function
def test():
a=[]
b=[]
c=[]
for m in message:
if 'IMPORTANT' in m.Subject:
#print(m.Subject)
#print(m.senton.date())
#print(m.Sender)
a += [m.Subject]
b += [m.senton.date()]
c += [m.Sender]
return pd.DataFrame(
data={'Subject':a,'Date':b,'Sender':c},
columns=['Subject','Date','Sender']
#data={'Subject':a,'Date':b},
#columns=['Subject','Date']
)
OutlookのアイテムはFor文で取り出す必要がある。今回はタイトルにImportantが入ったメールを対象にするためにIfで条件を絞っている。