ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • pyhton을 사용하여 엑셀 두개 중복 데이터 검사
    띵★/프로그램 개발 2019. 8. 14. 10:35
    반응형

    엑셀은 할줄 몰라도 파이썬은 할줄안다.

    사실 엑셀에 이러한 기능이 있을지도 모른다. 아마 없다면

    비베로 충분히 구현이 가능하지 않을까싶지만

    할줄모르기 때문에 파이썬으로 구현했었다. (아는사람의 부탁으로... 매번 눈으로 검사하는데 너무 힘들단다)

    #음..각종 라이브러리를 추가하궁

    from openpyxl import load_workbook 

    import openpyxl

    import win32com.client

    import win32com.client.gencache

    import os

    #디렉토리의 파일을 읽고 xls이면 실행한다.  뭐를? xls를 xlsx로 바꾼다.

    def rename_file(): 

        for filename in os.listdir("."): 

            if filename.endswith("xls"): 

                fname = os.getcwd()+"\\"+filename

                excel = win32com.client.gencache.EnsureDispatch('Excel.Application')

                wb = excel.Workbooks.Open(fname)

                wb.SaveAs(fname+"x", FileFormat = 51)    

                wb.Close()                               

                excel.Application.Qudmadmait()


    if __name__ == "__main__": 

         rename_file()


    # 음 파일이름에서 느껴지겠지만 무역회사 다니는 친구다. (사실 더 잘짤수있었는데 귀찮아서 진짜 대충짯다. 회사에서 비슷할거 짤때는 

    # GUI도 만들었다궁... 어쩃든 one.xlsx , two.xlsx는 비교할 엑셀 파일 두개다. 

    Buyer_invoice_filename= "one.xlsx" 

    V_filename="two.xlsx"

    #음..시트카운트를 왜썻더라... 기억이 안나지만 엑셀파일을 열어서 시트를 가져오는 소스다.

    sheetcount = int(0)

    wb = load_workbook(os.getcwd()+"\\"+Buyer_invoice_filename)

    sheetList = wb.get_sheet_names()       

    sheet = wb.get_sheet_by_name(sheetList[int(0)])

    #리스트 형식 만들어주려고 썻다.                      

    Invoice=[]

    Vina=[]

    #시트를 맥시범 가지고와서 셀에 넣는다

    InvoiceCount = sheet.max_row

    A_cell=sheet['A2':'A'+str(sheet.max_row)]

    for row in A_cell:

       for cell in row:

           Invoice.append(cell.value)

    #다음 비교할 엑셀 파일 가져오고      

    wb = load_workbook(os.getcwd()+"\\"+V_filename)

    sheetList = wb.get_sheet_names()

    #F6+str(sheet.max_row)는 F6부터 검사를 시작해서 맥시멈까지 돌리겠다는 뜻이다. 적절하게 수정하자. 

    for i in range(len(sheetList)):

          sheet = wb.get_sheet_by_name(sheetList[int(i)-1])

          F_cell=sheet['F6':'F'+str(sheet.max_row)]

          VinaCount=sheet.max_row

          for row in F_cell:

              for cell in row:

                  Vina.append(cell.value)

          for j in range(InvoiceCount-1):

              for j2 in range(VinaCount-5):

                  if(Invoice[j]==Vina[j2]):

                      sheet["F"+str(j2+5)].font=openpyxl.styles.Font(size=14, color="FF0000")

                      sheet["F"+str(j2+5)].number_format = sheet["F"+str(j2+5)].number_format

    wb.save(os.getcwd()+"\\"+" 되나.xlsx")

           #음 공통된게 있으면 사이즈를 변경하고 색상도 변경해준다. 그리고 결과를 저장해준다~ 

           

              


          끝 

    30분걸렸었나 그랬던거같다. 

    오래전에 짠거라 기억이 가물가물 



    반응형

    댓글

Designed by Tistory.