반응형

  1. # 1. 다음 단어장에서 엑셀로 데이터를 받는다.  
  2. # 2. 엑셀에서 단어를 알파벳 순으로 정렬을 해준다.  
  3. # 3. 이 프로그램을 돌리면 중복된 단어를 삭제해주고 찾아본 횟수를 +1 해준다.  
  4. # 4. 많이 찾은 단어를 7-5-3 순으로 빨강, 오렌지, 노랑 으로 색칙해준다.  
  5.   
  6. import win32com.client  
  7. import sys, signal, os, time  
  8.   
  9. WORD_COLMN = 1 # 1열에 단어가 나온다.  
  10. FINDCNT_COLMN = 7 # 7열에 찾은 횟수가 나온다.  
  11.   
  12. # Ctrl+c 로 끄기위해서 시그널 핸들러 등록한다.  
  13. def siginthandler(signum, frame):   
  14.     sys.exit()  
  15.   
  16. signal.signal(signal.SIGINT, siginthandler)  
  17.   
  18. excel = win32com.client.Dispatch("Excel.Application")  
  19. excel.Visible = True # 동작되는 것을 보면서 할라면 이렇게  
  20.   
  21. wb = excel.Workbooks.Open('C:\\Users\\hybri\\Downloads\\단어장 통합.xlsx'# arg로 받아도 됨.  
  22. ws = wb.ActiveSheet # 켜져있는 시트  
  23.   
  24. pre_word = ''  
  25. pre_cell_row_index = 0  
  26. pre_find_cnt = 0  
  27. cur_find_cnt = 0  
  28.   
  29. #전체 시트 사이즈 찍어본다.  
  30. print ('Sheet size : %d x %d'%(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count))  
  31.   
  32. # 전체를 훑음.  
  33. for r in ws.rows:  
  34.     # 끝까지 가거나 또는 빈칸이 나오면 멈춘다.  
  35.     if(r.row == ws.UsedRange.Rows.Count or ws.Cells(r.row, WORD_COLMN).Value is None):  
  36.         break  
  37.       
  38.     try:  
  39.         # 현재 행의 단어와 찾은 횟수 기록  
  40.         cur_word = ws.Cells(r.row, WORD_COLMN).Value  
  41.         cur_find_cnt = int(ws.Cells(r.row, FINDCNT_COLMN).Value)  
  42.   
  43.         # 많이 찾은 단어 색칠해서 강조해줌. 그렇지 않으면 그냥 투명처리함.  
  44.         colored_row = "A%d:G%d"%(r.row, r.row)  
  45.         if (cur_find_cnt >= 7):  
  46.             ws.Range(colored_row).Interior.ColorIndex = 46  
  47.             print('Red')  
  48.         elif (cur_find_cnt >= 5):  
  49.             ws.Range(colored_row).Interior.ColorIndex = 45  
  50.             print('Orrange')  
  51.         elif (cur_find_cnt >= 3):  
  52.             ws.Range(colored_row).Interior.ColorIndex = 44  
  53.             print('Yellow')  
  54.         else:  
  55.             ws.Range(colored_row).Interior.ColorIndex = 0  
  56.             print('Transparency')  
  57.           
  58.         # 현재 행의 단어랑 이전 행의 단어가 같으면 이전 행의 찾은 횟수 카운트에 더하고 중복행 지움.  
  59.         if(cur_word == pre_word):  
  60.             print('[%05d] %2d : %s +++ %d'%(r.row, cur_find_cnt, cur_word, pre_find_cnt+1 ))  
  61.             ws.Cells(pre_cell_row_index, FINDCNT_COLMN).Value = pre_find_cnt + ws.Cells(r.row, FINDCNT_COLMN).Value  
  62.               
  63.             del_row = "A%d:G%d"%(r.row, r.row)  
  64.             ws.Range(del_row).Value = ''  
  65.             # ws.Rows(r.row).EntireRow.Delete() # 원래는 행을 삭제할라고 했는데 행 삭제하면 인덱스가 꼬여서 그냥 내용만 지운다.  
  66.         else:  
  67.             print('[%05d] %2d : %s'%(r.row, cur_find_cnt, cur_word))  
  68.     except Exception as ex:  
  69.         print('ERROR >>> ', ex)  
  70.         pass  
  71.       
  72.     pre_word = cur_word  
  73.     pre_find_cnt = cur_find_cnt  
  74.     pre_cell_row_index = r.row  
  75.   
  76. #wb.SaveAs('c:\\Users\\Jason\\Desktop\\test.xlsx') # 사본으로 저장하면 좋겠다.  
  77. #excel.Quit() # 그냥 열린채로 보게 끄지 않는다.  
  78.   
  79. # https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2007/cc296089(v=office.12) 색상코드  

 

반응형

'Python' 카테고리의 다른 글

ubuntu 에서 python 버전 관리 2 or 3  (0) 2020.05.31
python3 virtualenv  (0) 2020.04.06
update-alternatives python  (0) 2019.02.22
Posted by Real_G