免责声明:我对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:类型&#39; pywintypes.datetime&#39;的参数不可迭代。我的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中找到匹配项,然后将该值附加到嵌套列表的末尾。我想要它做的是在它实际匹配的行/嵌套列表中匹配它。为长篇帖子道歉并感谢那些阅读它的人。
答案 0 :(得分:0)
没关系,我通过转换
解决了TypeError问题and i[4] in appointmentItem.Start:
# to
and i[4] in str(appointmentItem.Start):
此外,我事先重新格式化了我的CSV,现在它将与Outlook的格式相匹配。至于我的匹配是否附加在错误的行中,我想我会通过将匹配附加到单独的CSV /数据帧,然后将该数据帧连接到原始CSV /数据帧来解决这个问题。