2017-11-15 7 views
0

면책 조항 : 저는 Python을 처음 사용하는 사람입니다. 나는 CSV를 통해 스크립트를 작성하려고 시도하고 특정 열이 Outlook 일정 항목 (제목, 구성도 및 날짜 일치)과 일치하는지 확인한 다음 스크립트가 새 열에 성공적으로 일치한다는 것을 메모합니다 나는 this question에서 크게 훔쳤다. 아래는 나의 전체 대본이다.일치하는 Outlook 일정 날짜를 CSV로 변경하고 목록에 일치 항목 추가

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) 

그러나, 곧 내가 내 날짜 열에 일치하도록 노력으로 (내가 [4]),이 오류가 발생합니다 : 형식 오류가 형식 '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에서 일치하는 항목을 발견한다는 것입니다 다음 중첩리스트의 마지막에 그 값을 추가 : 같은 예를 들어, 내 출력이 보인다. 내가하고 싶은 것은 그것이 실제로 일치 된 행/중첩 목록에서 그것을 일치시키는 것입니다. 긴 게시물에 사과하고 그것을 읽은 사람들에게 감사드립니다.

답변

0

신경 끄시 고, 나는 내 CSV 사전 그래서 지금 Outlook의 형식과 일치합니다 포맷, 또한

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

를 변환하여 형식 오류를 해결했다. 내 경기가 잘못된 행에 추가되는 경우 개별 CSV/데이터 프레임에 일치 항목을 추가 한 다음 해당 데이터 프레임을 원본 CSV/데이터 프레임에 추가하여 해결할 것입니다.