Python實(shí)現(xiàn)對(duì)比兩個(gè)Excel數(shù)據(jù)內(nèi)容并標(biāo)記出不同
日常工作中需要對(duì)比兩個(gè)Excel工作表中的數(shù)據(jù)差異是很不方便的,使用python來(lái)做就比較簡(jiǎn)單了!
我們的思路是通過(guò)讀取兩個(gè)Excel的數(shù)據(jù),采用逐個(gè)遍歷對(duì)比同一個(gè)位置的兩個(gè)單元格如果不是相同的則打上對(duì)應(yīng)的標(biāo)記,處理完成后再另存為Excel文件。
既然是對(duì)每個(gè)單元格的操作那肯定離不開openpyxl模塊,沒有的話使用pip的方式安裝一下即可。
pip install openpyxl
接著我們準(zhǔn)備讀取兩個(gè)需要對(duì)比的Excel數(shù)據(jù)內(nèi)容,這里準(zhǔn)備的是data1.xlsx和data2.xlsx作為源數(shù)據(jù)使用。


然后,將openpyxl模塊及其所需要的樣式填充/文字設(shè)置相關(guān)的對(duì)象導(dǎo)入。
# It imports the PatternFill class from the openpyxl.styles module. from openpyxl.styles import PatternFill # It imports the colors class from the openpyxl.styles module. from openpyxl.styles import colors # It imports the Font class from the openpyxl.styles module. from openpyxl.styles import Font # It imports the openpyxl module and renames it as pxl. import openpyxl as pxl
下面使用openpyxl模塊的load_workbook函數(shù)讀取到Excel文件對(duì)象,并提取兩個(gè)Excel文件中'Sheet1'工作表作為源數(shù)據(jù)。
# It loads the data1.xlsx file and assigns it to the workbook_1 variable. workbook_1 = pxl.load_workbook(r'data1.xlsx') # It loads the data2.xlsx file and assigns it to the workbook_2 variable. workbook_2 = pxl.load_workbook(r'data2.xlsx') # Assigning the Sheet1 object to the workbook_1_sheet_1 variable. workbook_1_sheet_1 = workbook_1['Sheet1'] # It assigns the Sheet1 object to the workbook_2_sheet_1 variable. workbook_2_sheet_1 = workbook_2['Sheet1']
提取兩個(gè)工作表中的最大行和最大列,這樣即使兩個(gè)表的行數(shù)和列數(shù)不一致也能完全找出不同的單元格數(shù)據(jù)。
# A ternary operator. It is equivalent to: max_row = workbook_1_sheet_1.max_row if workbook_1_sheet_1.max_row > workbook_2_sheet_1.max_row else workbook_2_sheet_1.max_row # A ternary operator. It is equivalent to: max_column = workbook_1_sheet_1.max_column if workbook_1_sheet_1.max_column > workbook_2_sheet_1.max_column else workbook_2_sheet_1.max_column
使用for循環(huán)的方式分別遍歷行數(shù)據(jù)和列數(shù)據(jù),然后判斷對(duì)應(yīng)單元格的數(shù)據(jù)值是否相等,若是不相等則打上標(biāo)記。
for i in range(1, (max_row + 1)):
for j in range(1, (max_column + 1)):
cell_1 = workbook_1_sheet_1.cell(i, j)
cell_2 = workbook_2_sheet_1.cell(i, j)
if cell_1.value != cell_2.value:
cell_1.fill = PatternFill("solid", fgColor='FFFF00')
cell_1.font = Font(color=colors.BLACK, bold=True)
cell_2.fill = PatternFill("solid", fgColor='FFFF00')
cell_2.font = Font(color=colors.BLACK, bold=True)最后將對(duì)比處理完成后的工作表對(duì)象使用save函數(shù)進(jìn)行保存即可。
# It saves the workbook_1 object to the data3.xlsx file.
workbook_1.save('data3.xlsx')
# It saves the workbook_2 object to the data4.xlsx file.
workbook_2.save('data4.xlsx')下面是通過(guò)對(duì)比差異化處理后的data3.xlsx和data4.xlsx的工作表數(shù)據(jù)。


從結(jié)果來(lái)看,即使是兩個(gè)表的數(shù)據(jù)行數(shù)不一致也能對(duì)比出差異數(shù)據(jù)并打上了標(biāo)記。
到此這篇關(guān)于Python實(shí)現(xiàn)對(duì)比兩個(gè)Excel數(shù)據(jù)內(nèi)容并標(biāo)記出不同的文章就介紹到這了,更多相關(guān)Python對(duì)比Excel數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家
相關(guān)文章
Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼
這篇文章主要介紹了Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
python生成xml時(shí)規(guī)定dtd實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于python生成xml時(shí)規(guī)定dtd實(shí)例方法,需要的朋友們學(xué)習(xí)參考下。2020-09-09
用Python將IP地址在整型和字符串之間輕松轉(zhuǎn)換
這篇文章主要給大家介紹了利用Python將IP在整型和字符串之間輕松轉(zhuǎn)換的相關(guān)資料,文中還跟大家分享了Python下利用正則表達(dá)式來(lái)匹配校驗(yàn)一個(gè)字符串是否為ip地址的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03
跟老齊學(xué)Python之再深點(diǎn),更懂list
對(duì)于list,由于她的確非常非常龐雜,在python中應(yīng)用非常廣泛,所以,雖然已經(jīng)介紹完畢了基礎(chǔ)內(nèi)容,這里還要用一講深入一點(diǎn)點(diǎn),往往越深入越...2014-09-09
python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例
今天小編就為大家分享一篇python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
Python RPA自動(dòng)化機(jī)器人模擬鼠標(biāo)鍵盤
這篇文章主要介紹了Python RPA自動(dòng)化機(jī)器人模擬鼠標(biāo)鍵盤,RPA,全稱為Robotic Process Automation,即機(jī)器人流程自動(dòng)化。我們可以利用RPA技術(shù)將工作中可重復(fù)的部分流程化,讓機(jī)器替我們完成這一工作2023-02-02

