我有以下脚本,该脚本正在尝试从Outlook获取附件。
outlook = win32com.client.Dispatch("Outlook.Application")
inbox = outlook.GetDefaultFolder(0)
messages = inbox.Items
message = messages.GetLast() #open last message
attachments = message.Attachments #assign attachments to attachment variable
attachment = attachments.Item(1)
attachment.SaveASFile(os.path.join('c:', 'temp'))
但是,当我运行它时,我得到以下信息:
Traceback (most recent call last):
File "C:/Users/e003048/QA/trunk/automation/selenium/src/global_functions/util_get_email_attachments.py", line 11, in <module>
inbox = outlook.GetDefaultFolder(0)
File "C:\Python27\Lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Outlook.Application.GetDefaultFolder
我不确定我应该把用户名放在哪里使它工作。
我尝试了以下答案中的建议,并收到以下错误:
Traceback (most recent call last):
File "C:/Users/e003048/QA/trunk/automation/selenium/src/global_functions/util_get_email_attachments.py", line 10, in <module>
inbox = mapi.GetDefaultFolder(0)
File "<COMObject <unknown>>", line 2, in GetDefaultFolder
pywintypes.com_error: (-2147024809, 'The parameter is incorrect.', None, None)
我想包括完整的工作代码,以防其他人发现它有用:
def get_email_attachments(self):
outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace('MAPI')
# change the Folders parameter to the directory you are having the attachment go to
inbox = outlook.GetDefaultFolder(6).Folders('ForAttachments')
messages = inbox.Items
message = messages.GetLast() # opens the last message
attachments = message.Attachments
attachment = attachments.Item(1)
attachment.SaveAsFile('C:\\temp\\' + attachment.FileName)
在GetDefaultFolder
没有应用对象(见定义应用程序文档),它的定义NameSpace
,你通过得到的对象
mapi = outlook.GetNameSpace("MAPI")
然后
inbox = mapi.GetDefaultFolder(0)
我想你也可以用
mapi = outlook.Session
代替GetNameSpace
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句