0
votes

I have connected Outlook with Python 3.7 using win32com. I am trying to download attachments as per the subject line and sender id and saving the attachments to a specified location. I have been able to do this successfully by reading an excel file in python, the excel file consisting of subject line and destination (to save the attachments) in order to make the code more dynamic, however i still face one issue - when i have multiple emails with the same subject line on the same day, python is not able to save the attachments, as there is duplication of emails.

i have filtered my outlook inbox items on the basis of "today", but i wish to extract the most recent emails which i think can only be done on the basis of datetime considered together.

Example - Subject line is "test". i have two emails with this subject line on the same day at different times. How do i download the attachment from the most recent of these two emails? My excel file consists of just one entry of "test" subject line.

Any help would be greatly appreciated.

Thanks in advance!

val_date = datetime.date.today()

    for i in Items:
#        print(i)
        if i.Subject == file['Subject'][j] and i.ReceivedTime.date() == val_date:
          #  print(i, i.ReceivedTime)
            #j = j + 1
  #          print("j:", j)
            for attachment in i.Attachments:
                print(attachment.FileName)
1
Please show your existing code that does the filtering.Dmitry Streblechenko
I have edited my question. Hope this helps now!Deepankar Garg

1 Answers

0
votes

Firstly, never loop through all items in a folder - that is what Items.Find/FindNext and Items.Restrict are for.

Secondly, never use "==" operator with datetime values: the condition will never be satisfied even if you match the value down to a millisecond. And in your case it is just date. Always use a range.