最近媳婦工作上遇到一個重復性勞動,excel表格查重,重復的標記起來,問我能不能寫個程序讓它自動查重標記
必須安排
第一次正兒八經寫python,邊上網查資料,邊寫
終于成功了
在此記錄一下
首先安裝xlwings庫
1
|
pip install xlwings |
寫代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
import xlwings as xw # 輸入表名 title = input () # 指定不顯示地打開Excel,讀取Excel文件 app = xw.App(visible = False , add_book = False ) wb = app.books. open (title) # 打開Excel文件 sheet = wb.sheets[ 0 ] # 選擇第0個表單 # 獲取表行數 sheetInfo = sheet.used_range maxRow = sheetInfo.last_cell.row # maxColumn = sheetInfo.last_cell.column # print('表行數:',maxRow) # 單據編號 num = [] # 報銷類型 baoxiaoType = [] # 部門 department = [] # 收款方 name = [] # 報銷金額 money = [] # 將需要的數據讀取保存 for row in range ( 2 , maxRow): value = sheet. range ( "A" + str (row)).value num.append(value) value = sheet. range ( "C" + str (row)).value baoxiaoType.append(value) value = sheet. range ( "H" + str (row)).value department.append(value) value = sheet. range ( "N" + str (row)).value name.append(value) value = sheet. range ( "K" + str (row)).value money.append(value) # print(num) # print(baoxiaoType) # print(department) # print(name) # print(money) # 保存標記為重復的行號 flag = [] # 判斷是否已經標記為重復 # 重復返回Ture # 否則返回False def isRepeat(index): for num in flag: if num = = index: return True else : continue return False # 遍歷每一行,進行查重 for row in range ( 0 , len (money)): # 判斷是否已經標記為重復 # 如果重復不做判斷,結束本次循環 # 否則斷續向下執行 if True = = isRepeat(row + 2 ): continue elif False = = isRepeat(row + 2 ): # 獲取當前行數據 current = money[row] # 遍歷后面行是否和當前行數據重復 for subRow in range ( 1 , len (money)): # 獲取下一行數據 subCur = money[subRow] # 判斷當前行內容和對比行內容是否相等 if current = = subCur: # 再判斷編號行內容是否相等 if num[row] = = num[subRow]: continue else : # 對比其它內容是否相等 if ( (department[row] = = department[subRow]) and (baoxiaoType[row] = = baoxiaoType[subRow]) and (name[row] = = name[subRow]) ): # 將重復行行號保存,表格的表頭,且表頭行號從1 開始,所以行號等于當前索引+2 flag.append(subRow + 2 ) # 設置兩個重復行的首列單元格顏色 cell = sheet. range ( "A" + str (row + 2 )) cell.color = 0 , 255 , 255 subcell = sheet. range ( "A" + str (subRow + 2 )) subcell.color = 0 , 255 , 255 # 打印提示 print ( "重復起始行:" , row + 2 , "重復行" , subRow + 2 ) # 保存當前工作簿 wb.save() # 關閉當前工作簿 wb.close() # 退出excel程序 app.quit() # 阻塞不退出 input ( "Press Any Key" ) |
鑒于媳婦辦公電腦不方便安裝python環境,所以打包成exe可執行程序,使用pyinstaller工具
安裝
1
|
pip install pyinstaller |
打包
1
2
3
|
# -F 打包為單文件 # -i 指定圖標 pyinstaller -F *.py -i *.ico |
以上就是用python對excel查重的詳細內容,更多關于python excel查重的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/Doyoung/p/14077054.html