I receive a daily mail with the subject “XYZ” containing a CSV file with some information. I also got this python code which goes into my outlook account, looks for the mail with the subject “XYZ” and extracts the attachment and finally extends a certain database with the new information from the attachment. However, this code only gets the most recent email so that I need to run that code daily to keep it updated. If I’m not able to do that daily, lets say because I am on vacation, my database is going to miss some information from previous days.
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
val_date = date.date.today()
sub_today = 'XYZ'
att_today = 'XYZ.csv'
"""
Look for the email with the given subject and the attachment name
"""
for msg in all_inbox:
if msg.Subject == sub_today:
break
for att in msg.Attachments:
if att.FileName == att_today:
break
"""
save the update and extend the database
"""
att.SaveAsFile('U:/' + 'XYZ.csv')
read_path = "U:/"
write_path = "V:/Special_Path/"
df_Update = pd.read_csv(read_path + 'XYZ.csv',parse_dates=['Date'],skiprows=2)
del df_Update['Unnamed: 14']
df_DB_Old = pd.read_csv(write_path + 'DataBase_XYZ.csv',parse_dates=['Date'])
DB_DatumMax = df_DB_Old['Date'].max()
Upd_DatumMax = df_Update['Date'].max()
if df_DB_Old['Date'].isin(pd.Series(Upd_DatumMax)).sum()>0:
print(' ')
print('Date already exists!')
print(' ')
input("press enter to continue...")
# exit()
sys.exit()
else:
df_DB_New = pd.concat([df_DB_Old, df_Update])
df_DB_New.to_csv(write_path + 'XYZ.csv',index=False)
Now I would like to extend that code, so that it checks when the last time was the database was updated and then it should extract the information from all the emails with subject “XYZ” starting from the day it was last updated.
Example: I run the code on the 01.10.2022 Im on vacation for 2 days The database was last updated on 01.10 On 04.10 im back and I run the code again. The code will look for the email from 02.10 & from 03.10 and of course also for the latest mail 04.10 extract the csv and extend the database
My first idea is to create a new folder where all the mails with subject “XYZ” automatically are moved. [Done!] Now I would check for the latest Date in my database. And now I have no clue how to proceed. I guess I need to loop though my new folder but only starting with mails which havent been extracted to the databse.