다음 단어장 엑셀로 받은거 python 으로 정리하기
Python :
2020. 5. 7. 14:06
반응형
- # 1. 다음 단어장에서 엑셀로 데이터를 받는다.
- # 2. 엑셀에서 단어를 알파벳 순으로 정렬을 해준다.
- # 3. 이 프로그램을 돌리면 중복된 단어를 삭제해주고 찾아본 횟수를 +1 해준다.
- # 4. 많이 찾은 단어를 7-5-3 순으로 빨강, 오렌지, 노랑 으로 색칙해준다.
- import win32com.client
- import sys, signal, os, time
- WORD_COLMN = 1 # 1열에 단어가 나온다.
- FINDCNT_COLMN = 7 # 7열에 찾은 횟수가 나온다.
- # Ctrl+c 로 끄기위해서 시그널 핸들러 등록한다.
- def siginthandler(signum, frame):
- sys.exit()
- signal.signal(signal.SIGINT, siginthandler)
- excel = win32com.client.Dispatch("Excel.Application")
- excel.Visible = True # 동작되는 것을 보면서 할라면 이렇게
- wb = excel.Workbooks.Open('C:\\Users\\hybri\\Downloads\\단어장 통합.xlsx') # arg로 받아도 됨.
- ws = wb.ActiveSheet # 켜져있는 시트
- pre_word = ''
- pre_cell_row_index = 0
- pre_find_cnt = 0
- cur_find_cnt = 0
- #전체 시트 사이즈 찍어본다.
- print ('Sheet size : %d x %d'%(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count))
- # 전체를 훑음.
- for r in ws.rows:
- # 끝까지 가거나 또는 빈칸이 나오면 멈춘다.
- if(r.row == ws.UsedRange.Rows.Count or ws.Cells(r.row, WORD_COLMN).Value is None):
- break
- try:
- # 현재 행의 단어와 찾은 횟수 기록
- cur_word = ws.Cells(r.row, WORD_COLMN).Value
- cur_find_cnt = int(ws.Cells(r.row, FINDCNT_COLMN).Value)
- # 많이 찾은 단어 색칠해서 강조해줌. 그렇지 않으면 그냥 투명처리함.
- colored_row = "A%d:G%d"%(r.row, r.row)
- if (cur_find_cnt >= 7):
- ws.Range(colored_row).Interior.ColorIndex = 46
- print('Red')
- elif (cur_find_cnt >= 5):
- ws.Range(colored_row).Interior.ColorIndex = 45
- print('Orrange')
- elif (cur_find_cnt >= 3):
- ws.Range(colored_row).Interior.ColorIndex = 44
- print('Yellow')
- else:
- ws.Range(colored_row).Interior.ColorIndex = 0
- print('Transparency')
- # 현재 행의 단어랑 이전 행의 단어가 같으면 이전 행의 찾은 횟수 카운트에 더하고 중복행 지움.
- if(cur_word == pre_word):
- print('[%05d] %2d : %s +++ %d'%(r.row, cur_find_cnt, cur_word, pre_find_cnt+1 ))
- ws.Cells(pre_cell_row_index, FINDCNT_COLMN).Value = pre_find_cnt + ws.Cells(r.row, FINDCNT_COLMN).Value
- del_row = "A%d:G%d"%(r.row, r.row)
- ws.Range(del_row).Value = ''
- # ws.Rows(r.row).EntireRow.Delete() # 원래는 행을 삭제할라고 했는데 행 삭제하면 인덱스가 꼬여서 그냥 내용만 지운다.
- else:
- print('[%05d] %2d : %s'%(r.row, cur_find_cnt, cur_word))
- except Exception as ex:
- print('ERROR >>> ', ex)
- pass
- pre_word = cur_word
- pre_find_cnt = cur_find_cnt
- pre_cell_row_index = r.row
- #wb.SaveAs('c:\\Users\\Jason\\Desktop\\test.xlsx') # 사본으로 저장하면 좋겠다.
- #excel.Quit() # 그냥 열린채로 보게 끄지 않는다.
- # 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 |