4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【忘備録】win32comを使って、Outlookの添付ファイルを保存

Last updated at Posted at 2019-10-08

win32comを使ったOutlookの添付ファイル保存

  • Outlookのメールをpythonで指定のフォルダに保存
from win32com.client import Dispatch
import datetime as date

save_path = r'保存先フォルダパス'
lag = 1 # 過去何日分のメールを検索対象とするか
sub_ = '対象メールの件名'
att_ = '*添付ファイル名'

outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")

# サブフォルダも指定可
# inbox = inbox.Folders("sub-folder")

all_inbox = inbox.Items
val_date = (date.date.today() - date.timedelta(lag)).strftime("%d/%m/%y")

for msg in all_inbox:
    print(msg)

    if sub_ in msg.Subject:
        for att in msg.Attachments:
            if att_ in att.FileName:
                att.SaveAsFile(save_path + "/" + att.FileName)

    elif msg.SentOn.strftime("%d/%m/%y") < val_date:
        break
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?