Member-only story

PythonでOutlook の使い方を分析する

Naoki Satoh
8 min readJan 20, 2024

とあるプロジェクトの振り返りであまりにもメール多すぎというかコミュニケーションの難しさを感じたので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で条件を絞っている。

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Naoki Satoh
Naoki Satoh

Written by Naoki Satoh

オランダ在住 セキュリティエンジニア。専門はIT監査、IT統制、リスク。よわよわPythonista。コンサドーレ札幌サポ。タッチラグビー。語学マニア。マストドン mstdn.jp/@naokyneko Pixel7 Instagram www.instagram.com/naokisatoh_nl

No responses yet

Write a response