Python-属性错误(在其他PC上运行脚本)

时间:2018-06-26 10:59:14

标签: python email outlook

在此处填写初学者。 下面的脚本在PC1上运行良好,但在PC2上却无法正常运行。我收到以下信息:

Traceback (most recent call last):
  File "C:\Users\sgreene\Documents\indices.py", line 44, in <module>
    if msg.SentOn.strftime('20%y-%m-%d') == str(date.today() + datetime.timedelta(lagdays)):
  File "C:\Program Files\Python36-32\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.SentOn

据我所知,两个版本的pypiwin32(win32com)在两台PC上都是相同的(版本223),所以我不确定如何处理。

注意:我也正在寻找一个特定的邮件文件夹。目前,我已将其设置为默认收件箱(6)。如何使脚本搜索一个专门命名的邮件文件夹?

            from win32com.client import Dispatch
            import xml.etree.ElementTree as ET
            from datetime import date
            import pandas as pd
            import datetime
            import openpyxl as px
            import os


            file_location = r'C:\Users\Stefa\Documents\XMLfiles'


            excel_location = r'C:\Users\Stefa\Documents\indices.xlsx'


            sheet_code = 'Sheet1'


            lag = 3

            d = str(date.today())    

            outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
            inbox = outlook.GetDefaultFolder("6")
            all_inbox = inbox.Items
            sub_today = ['test1', 'test2', 'test3', 'test4']
            attach_strings = ['CMSIA', 'CMSRA']
            sheet = pd.read_excel(excel_location)
            wb = px.load_workbook(excel_location)
            ws = wb[sheet_code]


            for lagdays in range(-lag,1): 

                day = (date.today() + datetime.timedelta(lagdays)).weekday()
                newfolder = file_location + '\\' + str(date.today() + datetime.timedelta(lagdays))

                if not os.path.exists(newfolder): 
                    if day<5:
                        os.makedirs(newfolder)
                        for i in sub_today:
                            for x in attach_strings:
                                for msg in all_inbox:
                                    if msg.SentOn.strftime('20%y-%m-%d') == str(date.today() + datetime.timedelta(lagdays)):
                                        if msg.Subject == i:          
                                            for att in msg.Attachments:
                                                if x in att.FileName:
                                                    att.SaveAsFile(file_location + '\\' + str(date.today() + datetime.timedelta(lagdays)) + '\\' + i + x + '.xml')   
                                                    tree = ET.parse(file_location + '\\' + str(date.today() + datetime.timedelta(lagdays)) + '\\' + i + x + '.xml')
                                                    root = tree.getroot()
                                                    for child in root:
                                                        for u in range(1,ws.max_column):
                                                            if str(child[1].text) == str(ws.cell(row=1, column=u).value):
                                                                for n in range(1,ws.max_row):
                                                                    if str(ws.cell(row=n, column=1).value) == str(date.today() + datetime.timedelta(lagdays)) + ' 00:00:00':
                                                                        if x == 'CMSIA':
                                                                            ws.cell(row=n, column=u).value = child[4].text
                                                                        else: 
                                                                            ws.cell(row=n, column=u).value = child[2].text

            wb.save(r'C:\Users\Stefa\Documents\SMindices.xlsx')

1 个答案:

答案 0 :(得分:1)

您假设收件箱中只有MailItem个对象。您还可以拥有ReportItemMeetingItem对象-它们不公开SentOn属性。首先检查Class属性(所有OOM对象都将其公开)-对于MailItem对象,它是43(OlObjectClass.olMail)。

for msg in all_inbox:
  if msg.Class = 43 Then
   ...
  End If