从Outlook下载附件时出错

时间:2019-07-12 01:54:34

标签: python-3.x jupyter-notebook

我在尝试运行代码时收到此错误消息。关于如何解决此错误的任何想法?

这是用于将Outlook中的附件下载到我的笔记本电脑上的文件夹中。 我怀疑问题是由于文件名引起的,但我不太确定。 另外,getDefaultFolder(“ 6”)是什么意思?看到其他线程,它指向收件箱文件夹,但是我怎么知道呢?

from pandas.tseries.offsets import BDay

import pandas as pd

import win32com.client as win32

import datetime

import imaplib, email

import os.path

from datetime import date

import datetime as date

from win32com.client import Dispatch



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

inbox = outlook.GetDefaultFolder("6")

messages = inbox.Items

message = messages.GetFirst()

subject = message.Subject

get_path = 'X:\Reports\test\receive_mail'



for m in messages:

    if m.Subject == 'Hello':

        print (message)

        attachments = message.Attachments

        num_attach = len([x for x in attachments])

        for x in range(1, num_attach+1):

            attachment = attachments.Item(x)

            attachment.SaveASFile(os.path.join(get_path,attachment.FileName))

        print (attachment)

        message = messages.GetNext()



    else:

        message = messages.GetNext()

这是错误消息:

---------------------------------------------------------------------------

com_error                                 Traceback (most recent call last)

<ipython-input-22-fcbd5f6f0f56> in <module>()

     23         for x in range(1, num_attach+1):

     24             attachment = attachments.Item(x)

---> 25             attachment.SaveASFile(os.path.join(get_path,attachment.FileName))

     26         print (attachment)

     27         message = messages.GetNext()



C:\Program Files\Anaconda3\lib\site-packages\win32com\client\dynamic.py in SaveASFile(self, *args)



com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Cannot save the attachment. File name or directory name is not valid.', None, 0, -2147024773), None)

0 个答案:

没有答案