【问题标题】:Matching Outlook Calendar Dates to CSV Dates and Appending Match to List将 Outlook 日历日期与 CSV 日期匹配并将匹配附加到列表
【发布时间】:2018-04-29 05:29:27
【问题描述】:

免责声明:我对 Python 比较陌生。我正在尝试编写一个将通过 CSV 的脚本,检查某些列是否与 Outlook 日历项目匹配(按主题、组织者和日期匹配),然后让脚本注意到新列中存在成功匹配(我从this question 偷了很多东西。下面是我的整个脚本。

import win32com.client, datetime, re, os, csv, shutil

# set cwd, create copy of original CSV, and access outlook API
os.chdir('C:\\')
shutil.copy('masterCheck.csv', 'inspectCheck.csv')
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inspectors = {'ASMITH': 'Smith, Aaron', 'JDOE': 'Doe, Jane', 'KZEEBOP': 'Zeebop, Kim'}

#access csv and put it into a list
with open('inspectCheck.csv', 'r', newline = '', encoding='utf-8') as csvAppointments:
    reader = csv.reader(csvAppointments)
    masterList = list(reader)
    del masterList[-1] # delete blank nested list
    del masterList[1] #delete header
    for i in masterList: # switch out names so they can match Outlook descriptors later
        for key, value in inspectors.items():
            if i[3] in key:
                i[3] = value

# create another list for appending later in the script
finalList = []
finalList += masterList

# access the inspectors' calendars
x = 0
try:
    for inspector in inspectors.values():
        recipient = outlook.createRecipient(inspector)
        resolved = recipient.Resolve()
        sharedCalendar = outlook.GetSharedDefaultFolder(recipient, 9)
        codeAppointments = sharedCalendar.Items

        #restrict to items in the next year
        begin = datetime.date.today()
        end = begin + datetime.timedelta(days = 365);
        restriction = "[Start] >= '" + begin.strftime("%m/%d/%Y") + "' AND [End] <= '" +end.strftime("%m/%d/%Y") + "'"
        restrictedItems = codeAppointments.Restrict(restriction)

        # loop through inspectors' appointments and match
        for appointmentItem in restrictedItems:
            for i in masterList:
                addressSearch = i[1]
                if re.search(r'%s' % addressSearch, appointmentItem.Subject, re.IGNORECASE)\
                   and i[3] in appointmentItem.Organizer\
                   and i[4] in appointmentItem.Start:
                    finalList[x].append(appointmentItem.Subject)
                    x += 1
except IndexError:
    pass

# update spreadsheet
with open('inspectCheckFinal.csv', 'w', newline = '') as csvAppointments:
    appointmentsWriter = csv.writer(csvAppointments)
    appointmentsWriter.writerows(finalList)

我已成功将 CSV 中的列匹配到 Outlook 项目。例如,我可以让这个匹配。

if re.search(r'%s' % addressSearch, appointmentItem.Subject, re.IGNORECASE)

但是,一旦我尝试将其与我的日期列 (i[4]) 匹配,它就会引发错误:TypeError: 'pywintypes.datetime' 类型的参数不可迭代。我的 CSV 中的日期看起来像 2/11/2018 但 Outlook 中的日期(打印时)看起来像 2017-11-16 11:00:00+00:00。我不知道如何匹配这两者。

此外,我无法将 CSV 标记为成功匹配。虽然脚本会将一个值附加到每个嵌套列表的末尾(然后将其写入 CSV),但它不会将该值附加到 CSV 的匹配行。例如,我的输出如下所示:

Inspector     |Address     |Date     |Success?(print Address)
ASMITH        |212 Clark St|11/21/18 |Yes. 33 Blart Ave
ASMITH        |33 Blart Ave|11/20/18 |Yes. 212 Clark St

我的猜测是我的脚本在 Outlook 中找到了匹配项,然后将该值附加到嵌套列表的末尾。我想做的是在实际匹配的行/嵌套列表中匹配它。为这篇长文道歉,并感谢阅读它的人。

【问题讨论】:

    标签: python csv outlook calendar


    【解决方案1】:

    没关系,我通过转换解决了 TypeError

    and i[4] in appointmentItem.Start:
    # to
    and i[4] in str(appointmentItem.Start):
    

    此外,我事先重新格式化了我的 CSV,以便它现在可以匹配 Outlook 的格式。至于我的匹配项被附加在错误的行中,我想我会通过将匹配项附加到单独的 CSV/数据帧,然后将该数据帧连接到原始 CSV/数据帧来解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-21
      • 2021-08-29
      • 2023-03-31
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多