I have the below code that downloads he attachments from the outlook. But I'm having two problems. 1. Attachment wont be downloaded if there is already a file with the same name in the folder. I want it to be replaced with the new file (file names are same but with updated data). 2. The code is downloading all the images that it finds in the email. Its not a big problem, I can write a extra small code for it but I wanted to learn if its possible to exclude images inside the code I have.
import os
import win32com.client
path = r"C://Users//greencolor//Desktop//Autoreport//Load_attachments//"
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
def save_attachments(subject_prefix): # changed parameter name
messages.Sort("[ReceivedTime]", True) # sort by received date: newest to oldest
for message in messages:
if message.Subject.startswith(subject_prefix): # changed test
print("saving attachments for:", message.Subject)
for attachment in message.Attachments:
print(attachment.FileName)
attachment.SaveAsFile(os.path.join(path, str(attachment.FileName))) # changed to file-name
return
save_attachments('PB Report North Cluster - ' )