

TypeError: coercing to Unicode: need string or buffer, instance foundĪtt.SaveAsFile('new.xlsx') has no error, but there is no such file in the working directory. Raise AttributeError("%s.%s" % (self._username_, attr)) Outlook = Dispatch("Outlook.Application").GetNamespace("MAPI") str.Im trying to read email and download the attachment to my own folder using win32com module in Python, I stopped at getting the attachment object: from win32com.client import Dispatch.While message: # to test if there is a (last) message at all Return # exit after first matched messageĪlternatively use GetLast() if you know the messages only contain desired ones, sorted by date. Print("saving attachments for:", message.Subject)Īttachment.SaveAsFile(os.path.join(path, str(attachment.FileName))) # changed to file-name Messages.Sort("", True) # sort by received date: newest to oldest SolutionĪn example solution could be as follows (see comments for adjustments): def save_attachments(subject_prefix): # changed parameter name However this would deserve another question, own research and further specification and focus. Or you could parse the dates in message's subject and sort them accordingly. You could use GetLast() or Sort() with appropriate message property to filter on the newest sent report. You could instead use string-method startswith on the message subject like (subject_prefix)Īnd call your method with a common prefix like save_attachments('PB Report - ').įurthermore use the attachment.FileName to construct the output-file path. Python 2.7/3.6 compatible: GMAIL Accounts: A script to download email attachments from specific email addresses: Please edit the following details to work: YOUREMAILADDRESS: YOUREMAILPASSWORD: LABEL - Inbox, Trash, Archive. The wildcard for parameter subject wont work because parameter subject is used as string when comparing for equality in message.Subject = subject. If 'PB' in message.subject and 'Report' in message.subject: I also have the alternative code but when i run this code i never get the result nor error. Outlook = ("Outlook.Application").GetNamespace("MAPI")Īttachment.SaveAsFile(os.path.join(path, str(attachment))) Path = r"C:/Users/greencolor/Desktop/Autoreport/" I tried wildcard(*) as save_attachments('PB Report*') but did not work. Also, how do I tell python to search the subject which starts with PB Report and dont look at the rest of the title. I would like to implement the GetLast in this code in order to get only the newest sent report. For example I received email this Monday with subject PB Report -, last week PB Report - and so on.

Constant part of the subject title is PB Report and will have the Monday's date. Every Monday I receive the attachments with the slightly changed subject title.
