【问题标题】:How to clear the AttributeError for win32com of "object has no attribute 'GetNameSpace'"如何清除“对象没有属性'GetNameSpace'”的win32com的AttributeError
【发布时间】:2021-01-25 18:14:10
【问题描述】:

我正在尝试在特定时间范围内下载电子邮件和 pdf 附件,但是当尝试在 Python 中运行它时,我收到错误消息:“AttributeError: '' 对象没有属性 'GetNameSpace'"

上周它曾经工作过,但由于我也没有接触过它,所以不确定代码出了什么问题。在下面分享我的整个代码。

import win32com.client
import os
import time
import datetime as dt
import re

DateFilter = dt.datetime.now() - dt.timedelta(days = 2)

#Outlook MAPI
outlook = win32com.client.Dispatch("Outlook.Application").GetNameSpace("MAPI")
#Inbox Folder
inbox = outlook.GetDefaultFolder(6)
#Download Path
path = os.path.expanduser("~\\Desktop")
#Sort emails in inbox
messages = inbox.Items
messages.Sort("[ReceivedTime]", True)
#Filter emails to go through
DateFilterMsg = messages.Restrict("[ReceivedTime] >= '" + DateFilter.strftime('%m/%d/%Y %H:%M %p')+"'")

for message in DateFilterMsg:
    #Saving pdf attachments and their emails
    for attachment in message.attachments:
        if attachment.FileName.find(".pdf") > -1:
            attachment.SaveAsFile(f"{path}\\Email Downloader\\attachments\\{attachment.FileName}")
            emailname = str(message.subject)
            emailname = re.sub('[^A-Za-z0-9]+', ' ', emailname)
            print(emailname)
            mailName = f"{path}\\Email Downloader\\emails\\{emailname}.msg"
            message.SaveAs(mailName)

【问题讨论】:

    标签: python outlook


    【解决方案1】:

    回答问题以将其关闭

    GetNameSpace = GetNamespace
    message.attachments = message.Attachments
    mesasge.subject = message.Subject
    

    【讨论】:

    • 您好,我也遇到了同样的问题。你能解释一下这个答案是什么意思吗?
    【解决方案2】:

    请按照爱德华所说,尝试以下操作:

    import win32com.client
    import os
    import time
    import datetime as dt
    import re
    
    DateFilter = dt.datetime.now() - dt.timedelta(days = 2)
    
    #Outlook MAPI
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    #Inbox Folder
    inbox = outlook.GetDefaultFolder(6)
    #Download Path
    path = os.path.expanduser("~\\Desktop")
    #Sort emails in inbox
    messages = inbox.Items
    messages.Sort("[ReceivedTime]", True)
    #Filter emails to go through
    DateFilterMsg = messages.Restrict("[ReceivedTime] >= '" + DateFilter.strftime('%m/%d/%Y %H:%M %p')+"'")
    
    for message in DateFilterMsg:
        #Saving pdf attachments and their emails
        for attachment in message.Attachments:
            if attachment.FileName.find(".pdf") > -1:
                attachment.SaveAsFile(f"{path}\\Email Downloader\\attachments\\{attachment.FileName}")
                emailname = str(message.Subject)
                emailname = re.sub('[^A-Za-z0-9]+', ' ', emailname)
                print(emailname)
                mailName = f"{path}\\Email Downloader\\emails\\{emailname}.msg"
                message.SaveAs(mailName)
    

    【讨论】:

      猜你喜欢
      • 2015-11-10
      • 2018-02-21
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      相关资源
      最近更新 更多