Python 从outlook中的特定电子邮件中提取csv附件

Python 从outlook中的特定电子邮件中提取csv附件,python,python-3.x,outlook,extract,attachment,Python,Python 3.x,Outlook,Extract,Attachment,我是python初学者,试图从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 = 'I

我是python初学者,试图从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, 0, -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()

欢迎来到StackOverflow!失败是什么?运行时是否出现异常,或者程序只是没有效果?