【问题标题】:Error while saving attachment保存附件时出错
【发布时间】:2013-05-24 20:09:29
【问题描述】:

我是 Python 新手,遇到了一个我无法克服的错误。

如果主题与给定字符串匹配,则编写代码以检查我的前景并提取附件(excel)。代码如下:

import win32com.client

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

inbox = outlook.GetDefaultFolder(6)

print "Inbox name is:", inbox.Name

messages = inbox.Items
message = messages.GetFirst ()
while message:
    if message.Subject.startswith('EOD Report'):
        attachments = message.Attachments
        if attachments.Count>=1:
            attachment = attachments.Item(1)
            filename = 'c:\Users\xx\Python\%s'%attachment
            print filename
            attachment.WriteToFile(filename)
    message = messages.GetNext()

如果我去掉 'attachment.WriteToFile(filename)',它运行得非常好。但是,该特定语句会产生错误:

Traceback (most recent call last):
  File "C:\Users\xx\.spyder2\.temp.py", line 31, in <module>
    attachment.WriteToFile(filename)
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 522, in    __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Item.WriteToFile

有人知道出了什么问题吗?谢谢

【问题讨论】:

  • 听起来像 WriteToFile 不是可用的方法。试试加个print dir(attachment)看看有没有类似的,或者直接用open(filename,'w')等写出附件。

标签: python attributes outlook attachment writetofile


【解决方案1】:

代替:

attachment.WriteToFile(filename)

尝试:

attachment.SaveAsFile(filename)

我认为WriteToFile 用于从 Exchange 服务器本身检索附件。

SaveAsFile 用于保存从 Outlook 本地读取的附件。

【讨论】:

    【解决方案2】:

    AttributeError 告诉您 Item 对象没有称为 WriteToFile 的方法(或函数)。

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      相关资源
      最近更新 更多