How do I save an email attachement as a tempfile? FYR I am trying to read data from a zipped xls attachment and I don't need both the xls and zip file in the end. Thank you.
My code fyr: (Right now I think I am just saving the attachment with a tempfile name as a normal file. You can see I even have to os.remove the saved attachment at the end)
import tempfile
import win32com.client
import zipfile
import os
#from openpyxl import Workbook, load_workbook
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders("John@gmail.com").Folders("Inbox")
msgs = inbox.Items
msgs.Sort('ReceivedTime',True)
tf=tempfile.TemporaryFile()
tfn=tempfile.TemporaryFile().name
td=tempfile.gettempdir()
for msg in msgs:
atts=msg.Attachments
if "ABCD" in str(msg.Subject):
for att in atts:
att.SaveAsFile (tfn)
break
file=zipfile.ZipFile(tfn)
file.extractall(td)
file.close()
os.remove(tfn)