簡體   English   中英

將Outlook日歷日期與CSV日期匹配,並將匹配項追加到列表

[英]Matching Outlook Calendar Dates to CSV Dates and Appending Match to List

免責聲明:我是Python的新手。 我正在嘗試編寫一個將通過CSV的腳本,檢查某些列是否與Outlook日歷項匹配(按主題,組織者和日期匹配),然后讓腳本注意新列中存在成功的匹配項(我從這個問題中偷了很多)。 以下是我的整個腳本。

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中找到一個匹配項,然后將該值附加到嵌套列表的末尾。 我想做的是在實際匹配的行/嵌套列表中對其進行匹配。 很長的帖子表示歉意,並感謝那些閱讀它的人。

沒關系,我通過轉換解決了TypeError

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

此外,我預先重新格式化了CSV格式,因此它現在可以與Outlook的格式匹配。 至於我的匹配項被添加到錯誤的行中,我想我可以通過將匹配項添加到單獨的CSV /數據幀,然后將該數據幀連接到原始CSV /數據幀來解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM