Python 使用循环将 outlook 颜色类别添加到特定电子邮件

Python adding outlook color categories to specific emails with a loop

我正在尝试根据电子邮件对象 and/or 发件人电子邮件地址等条件向给定 Outlook 文件夹中的现有电子邮件添加颜色类别。

import win32com.client as client
import win32com
import pandas as pd

outlook = client.Dispatch("Outlook.Application").GetNamespace('MAPI')

main_account = outlook.Folders.Item(1)
second_account = outlook.Folders.Items(3)

df = pd.read_excel (r'C:\Python\test.xls')

df_outlook_folder = df['Outlook_folder'].tolist()
df_mail_object = df['Mail_object'].tolist()

out_iter_folder = main_account.Folders['Inbox'].Folders['TEST']

fixed_item_count = out_iter_folder.Items.Count
item_count = out_iter_folder.Items.Count

if yout_iter_folder.Items.Count > 0:
    for i in reversed(range(0,item_count)):
        message = out_iter_folder.Items[i]
        for y,z in zip(df_mail_object,df_outlook_folder):
            try:
                if y in message.Subject:
                     message.Move(second_account.Folders['Inbox'].Folders['TESTED'].Folders[z]
            except:
                pass
item_count = out_iter_folder.Items.Count
print('Nb mails sorted:',fixed_item_count - item_count)

上面的代码使我能够根据邮件对象移动电子邮件,但到目前为止我无法添加功能来更改 outlook 颜色类别

到目前为止,我花时间在以下文档上但没有成功 https://docs.microsoft.com/en-us/office/vba/api/outlook.categories

Categories 是已分配给 Outlook 项目的带分隔符的类别名称字符串。

mail.Categories='Red category'
mail.Save()

您可能会发现 话题很有帮助。