从2019 Outlook中的特定电子邮件中提取CSV附件

问题描述

我是python初学者,尝试从2019 Outlook中的选定电子邮件提取csv附件。 下面是我的代码,但失败了。

有人会帮助您吗?非常感谢。

import win32com.client

EMAIL_ACCOUNT = 'xxxxxxxx@importantcompany.com'  # e.g. 'good.employee@importantcompany.com'
ITER_FOLDER = 'InBox'  # e.g. 'IterationFolder'
MOVE_TO_FOLDER = 'InBox'  # e.g 'ProcessedFolder'
SAVE_AS_PATH = r'C:\DownloadedCSV'  # e.g.r'C:\DownloadedCSV'
EMAIL_SUBJ_SEARCH_STRING = 'xxxx Room Type'  # e.g. 'Email to download'


def find_download_csv_in_outlook():
    out_app = win32com.client.gencache.Ensuredispatch("outlook.application")
    out_namespace = out_app.GetNamespace("MAPI")

    out_iter_folder = out_namespace.Folders[EMAIL_ACCOUNT].Folders[ITER_FOLDER]
    out_move_to_folder = out_namespace.Folders[EMAIL_ACCOUNT].Folders[MOVE_TO_FOLDER]

    char_length_of_search_substring = len(EMAIL_SUBJ_SEARCH_STRING)

    # Count all items in the sub-folder
    item_count = out_iter_folder.Items.Count

    if out_iter_folder.Items.Count > 0:
        for i in range(item_count,-1):
            message = out_iter_folder.Items[i]

            # Find only mail items and report,note,meeting etc items
            if '_MailItem' in str(type(message)):
                print(type(message))
                if message.Subject[0:char_length_of_search_substring] == EMAIL_SUBJ_SEARCH_STRING \
                        and message.Attachments.Count > 0:
                    for attachment in message.Attachments:
                        if attachment.FileName[-3:] == 'csv':
                            attachment.SaveAsFile(SAVE_AS_PATH + '\\' + attachment.FileName)
                            message.Move(out_move_to_folder)
    else:
        print("No items found in: {}".format(ITER_FOLDER))


if __name__ == '__main__':
    find_download_csv_in_outlook()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)