【发布时间】: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