2016-08-17 2 views
0

Excel 워크 시트의 J 열에 비어 있지 않은 값을 찾는 다음 코드를 작성했습니다. K 열에 값의 전자 메일 주소를 가져 오는 것을 포함하여 일부 작업을 수행합니다. 그런 다음 smtp를 사용하여 구성원에게 전자 메일을 보냅니다.Python을 사용하여 Excel 시트의 목록 항목 찾기

대신 코드의 시작 부분에서 선언 할 수있는 Python 목록에서 전자 메일을받는 것이 좋습니다. 목록마다 워크 시트의 J 열에서 일치하는 이름을 찾은 다음 목록에서 결과 이메일 주소를 가져 오는 방법을 알아낼 수 없습니다.

끔찍한 구문을 사용하지 마십시오 ... 이것은 주요 파이썬 프로젝트의 첫 번째 찌르기입니다.

memlist = {'John Frank':'[email protected]', 
      'Liz Poe':'[email protected]'} 

try: 
    for i in os.listdir(os.getcwd()): 
     if i.endswith(".xlsx") or i.endswith(".xls"): 
      workbook = load_workbook(i, data_only=True) 

     ws = workbook.get_sheet_by_name(wsinput) 
     cell_range = ws['j3':'j7'] 
     for row in cell_range: # This is iterating through rows 1-7 
       #for matching names in memlist 
       for cell in row: # This iterates through the columns(cells) in that row 
        value = cell.value 
        if cell.value: 
         if cell.offset(row=0, column =-9).value.date() == (datetime.now().date() + timedelta(days=7)): 
          #print(cell.value) 
          email = cell.offset(row=0, column=1).value        
          name = cell.value.split(',',1)[0] 

답변

0

이것은 대답을 시도한 것입니다.

memlistkey : value 쌍을 포함하고 있기 때문에 이 아니고 dict입니다.

dict에 특정 키가 있는지 확인하려면 dict.has_key(key) 메서드를 사용할 수 있습니다.

memlist에서 이름은 key이고 해당 전자 메일은 value입니다. 코드에서

, 당신이 할 수있는이가 작동하는지 확인

if cell.value in memlist: # For Python 3 

을 :

if memlist.has_key(cell.value): # For Python 2 
    if ... # From your code 
     email = memlist[cell.value] 

를 파이썬 3 사용하고있는 경우,이 같은 키를 검색 할 수 있습니다 내가 너의 질문을 완전히 이해할 수 없었던 것처럼 너.

+0

저는 파이썬 3.5를 사용하고 있기 때문에 has_key가 작동하지 않는다고 생각합니다. –

0

Shubham, 나는 내 대답을 찾기 위해 응답의 일부를 사용했습니다. has_key 메소드 대신에 for/in 문과 후속 if 문을 사용했습니다.

하지만 두려움은 이러한 여러 가지 이유로 인해 코드 실행에 오랜 시간이 걸리고 가장 효율적이거나 최적이 아닐 수 있습니다. 그러나 그것은 또 다른 날의 가치가 있습니다.

try: 
for i in os.listdir(os.getcwd()): 
    if i.endswith(".xlsx") or i.endswith(".xls"): 
     workbook = load_workbook(i, data_only=True) 

     ws = workbook.get_sheet_by_name(wsinput) 
     cell_range = ws['j3':'j7'] 
     for row in cell_range: # This is iterating through rows 1-7 
       for cell in row: # This iterates through the columns(cells) in that row 
        value = cell.value 
        if cell.value: 
         if cell.offset(row=0, column =-9).value.date() == (datetime.now().date() + timedelta(days=7)): 
          for name, email in memlist.items(): 
           if cell.value == name: 
            #send the email 
관련 문제