工做關係須要比較兩個excel 文件中的差別,excel文件中數據較多,人工比對易出錯,且效率低。html
在網上學習了不少,並根據本身的一經驗和Beyound Compare 工具配合使用,效果不錯。ide
其中主要使用xlrd模塊,能夠直接對excel中數據進行提取做爲字符串使用工具
1 ################################################################# 2 # 3 # 4 # diff_excel.py 5 # 6 # example: diff_excel.py 7 # 8 # auther: Devin Li 9 # date: 2016/8/29 10 # function: compare excel files 11 # 12 ################################################################# 13 14 from xlrd import open_workbook 15 import sys 16 from tkinter import * 17 import tkinter.filedialog 18 import subprocess 19 20 BCompare_Path="C:\Program Files (x86)\Beyond Compare 3\BCompare.exe" 21 Log_Path='./report' 22 23 #=================================================================================== 24 def Input(): 25 26 global file1,file2 27 label = Label(Tk(),text = '選擇第一個對比文件') 28 label.pack() 29 file1=tkinter.filedialog.askopenfilename(filetypes=[("xlsx格式,xls格式","xlsx xls"),("All files","*")],title='選擇第一個對比文件') 30 31 label = Label(Tk(),text = '選擇第二個對比文件') 32 label.pack() 33 file2=tkinter.filedialog.askopenfilename(filetypes=[("xlsx格式,xls格式","xlsx xls"),("All files","*")],title='選擇第二個對比文件') 34 35 #=================================================================================== 36 37 #=================================================================================== 38 def Diff(): 39 40 41 global date1,date2 42 date1='' 43 date2='' 44 wb1 = open_workbook(file1) 45 wb2 = open_workbook(file2) 46 47 sheet1 = wb1.sheet_by_index(0) 48 sheet2 = wb2.sheet_by_index(0) 49 50 #print('file: %s \n sheet: %s rows: %d cols: %d \n' %(file1,sheet1.name, sheet1.nrows, sheet1.ncols)) 51 #print('file: %s \n sheet: %s rows: %d cols: %d \n' %(file2,sheet2.name, sheet2.nrows, sheet2.ncols)) 52 53 if (sheet1.ncols != sheet2.ncols): 54 print('Numbers of OS changed \n') 55 exit() 56 57 for col in range(3,sheet1.ncols): 58 if ( sheet1.cell_value(0,col) != sheet2.cell_value(0,col) ): 59 print('Items for OS changed \n') 60 print('item1: %s \n item2: %s \n'%(sheet1.cell_value(0,col),sheet2.cell_value(0,col))) 61 exit() 62 63 for col in range(3,sheet1.ncols): 64 if ('Windows' not in sheet1.cell_value(0,col) and 'RHEL' not in sheet1.cell_value(0,col) and 'SLES' not in sheet1.cell_value(0,col)): 65 continue 66 if ('X86' in sheet1.cell_value(0,col)): 67 continue 68 if ('Windows Utility' in sheet1.cell_value(0,col)): 69 continue 70 # if ('Windows SBS 2011 X64' in sheet1.cell_value(0,col)): 71 # continue 72 for row in range(1,sheet1.nrows): 73 if ( sheet1.cell_value(row,col) != sheet2.cell_value(row,col) ): 74 date1 = date1 + '#'*80 + '\n' + sheet1.cell_value(0,col) + '\n' + sheet1.cell_value(row,2) + '\n' + sheet1.cell_value(row,0) + '\n' + '#'*80 + '\n' 75 date1 = date1 + 'Current row: %d col: %d\n'%(row,col) +'\n' 76 date1 = date1 + sheet1.cell_value(row,col) + '\n' 77 78 date2 = date2 + '#'*80 + '\n' + sheet2.cell_value(0,col) + '\n' + sheet2.cell_value(row,2) + '\n' + sheet1.cell_value(row,0) + '\n' + '#'*80 + '\n' 79 date2 = date2 + 'Current row: %d col: %d\n'%(row,col) +'\n' 80 date2 = date2 + sheet2.cell_value(row,col) + '\n' 81 82 #=================================================================================== 83 84 85 #=================================================================================== 86 def Log(file1_name,file2_name): 87 88 global log1_name,log2_name,report_html 89 log1_name_init = file1_name.split('/')[-1].split()[-2] 90 log2_name_init = file2_name.split('/')[-1].split()[-2] 91 92 version1 = log1_name_init.split('_')[-1] 93 version2 = log2_name_init.split('_')[-1] 94 95 log1_name = Log_Path + '/' + log1_name_init + '_VS_' + version2 + '.txt' 96 log2_name = Log_Path + '/' +log2_name_init + '_VS_' + version1 + '.txt' 97 report_html = Log_Path + '/' +log1_name_init + '_VS_' + version2 + '.html' 98 99 100 log1 = open(log1_name, 'w') 101 log2 = open(log2_name, 'w') 102 103 log1.write(date1) 104 log2.write(date2) 105 106 log1.close() 107 log2.close() 108 109 return 0 110 #=================================================================================== 111 112 #=================================================================================== 113 def Bcompare(log1_name,log2_name): 114 cmd = BCompare_Path + ' ' + log1_name + ' ' + log2_name 115 p = subprocess.Popen(cmd) 116 #=================================================================================== 117 118 #=================================================================================== 119 def Bcompare_Script(): 120 121 script = '''text-report layout:side-by-side & 122 options:ignore-unimportant,display-context & 123 output-to:%3 output-options:html-color %1 %2''' 124 125 script_txt = open('script.txt', 'w') 126 script_txt.write(script) 127 script_txt.close() 128 129 return 0 130 #=================================================================================== 131 132 #=================================================================================== 133 def Bcompare_Report(file1,file2,report): 134 135 cmd = BCompare_Path + ' ' + '@script.txt ' + file1 + ' ' + file2 + ' ' + report 136 p = subprocess.Popen(cmd) 137 #=================================================================================== 138 139 140 def main(): 141 142 #file1 = sys.argv[1] 143 #file2 = sys.argv[2] 144 145 Input() 146 Diff() 147 148 Bcompare_Script() 149 150 151 if (Log(file1,file2) == 0): 152 153 Bcompare_Report(log1_name,log2_name,report_html) 154 Bcompare(log1_name,log2_name) 155 156 print('Pass') 157 else: 158 print('Fail') 159 160 161 if __name__=='__main__': 162 #if len(sys.argv)<3: 163 # exit() 164 main()