Reputation: 4398
I have a .csv attachment that is emailed to me daily. I'd like to read in this email using python and perform some modifications on it. The emails are sent to my Outlook email account.
This is what I am doing:
import win32com.client
my_outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folder = outlook.GetDefaultFolder(6) #index
for item in folder.Items
print(item.body)
However, this is for extracting data within the email, how would I read the actual attachment that is being sent? I am looking into extract-msg PyPi as well.
Any insight will be helpful.
Upvotes: 1
Views: 1421
Reputation: 75
To read the attachment, use the following..
import win32com.client
import datetime
import os
import email
outlook = win32com.client.Dispatch("outloook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # change depending on folder you wish to see
message = inbox.items
for message in inbox.Items:
if message.Unread == True # finds unread mesages
for attachment in message.Attachments:
This will show you all unread email attachments, simply complete the code with the file address you wish to save the attachments..
Upvotes: 2