I am looking to flag emails based on the sender on a shared outlook mailbox as quick as possible.
#imports:
import time
from time import strftime
import pandas as pd, win32com.client as client
from win32.com.client import Dispatch
#importing the excel file that contains email addresses and corresponding flags:
df_excel = pd.read_excel(r'\\user\...\addresses.xlsx')
#adding both columns as lists:
df_excel_mail = df_excel['mail'].tolist();df_excel_flag = df_excel['flag'].tolist()
outlook = client.Dispatch('Outlook.Application').GetNamespace('MAPI')
main_account = outlook.Folders.Item(1)
folder_inbox = main_account.Folders['Inbox'].Folders['Test']
folder_inbox_WIP = main_account.Folders['Inbox'].Folders['Test'].Folders['WIP']
while True:
time.sleep(0)
messages = folder_inbox.Items.Count
if messages > 0:
for i in reversed(range(0,messages)):
message = folder_inbox.Item[i]
for y, z, in zip(df_excel_mail,df_excel_flag)
if message.Categories == '' and y == message.SenderEmailAddress
message.Categories = z
message.Save
message.Move(folder_inbox_WIP)
messages_v2 = folder_inbox_WIP.Items.Count
if folder_inbox_WIP .Items.Count > 0:
for ii in reversed (range(0,messages_v2)):
message_v2 = folder_inbox_WIP[ii]
message_v2.Move(folder_inbox)
if strftime('%H, %M, %N') >= strftime('18:00:00')
break
my addresses.xlsx sheet looks like that:
| flag | |
|---|---|
| [email protected] | red color |
| [email protected] | green color |
| ... | ... |
| [email protected] | |
| ... | ... |
When an email address is available in the df_excel_mail list but corresponding flag color is missing in the df_excel_flag list. some emails are sent to WIP folder anyway and flagged as #QNAN.
How is this possible when the following code is supposed to prevent that to happen ?
if message.Categories == '' and y == message.SenderEmailAddress
0m3r
12.5k15 gold badges40 silver badges77 bronze badges
1 Answer 1
The problem was related to 'NaN' values in the dataframe I used .notna() to fix it
Sign up to request clarification or add additional context in comments.
Comments
lang-py
message.Categoriesis the flag/category attributed to an email in Outlookmessage.SenderEmailAddressis the email address stored in the excel sheet that I am looking to match with the email currently being scanned for potential flaggingmessage.Categories can be = ''if no flag assigned or to 'red color', 'green color', ... it works also with custom nammed categories. message.SenderEmailAddress is equal to address for current email being scanned by the loop y is equal to the mail column in the excel sheet