gpt4 book ai didi

python-2.7 - 使用 Python 从 Outlook 2010 获取附件

转载 作者:行者123 更新时间:2023-12-05 03:14:41 24 4
gpt4 key购买 nike

我有以下脚本,它试图从 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 没有在应用程序对象上定义(参见 application docs ),它是在您通过

获得的 NameSpace 对象上定义的
mapi = outlook.GetNameSpace("MAPI")

然后

inbox = mapi.GetDefaultFolder(0)

我觉得你也可以用

mapi = outlook.Session

代替 GetNameSpace

关于python-2.7 - 使用 Python 从 Outlook 2010 获取附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23700338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com