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è)單元格的操作那肯定離不開(kāi)openpyxl模塊,沒(méi)有的話使用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)文章
用PyInstaller把Python代碼打包成單個(gè)獨(dú)立的exe可執(zhí)行文件
這篇文章主要介紹了用PyInstaller把Python代碼打包成單個(gè)獨(dú)立的exe可執(zhí)行文件,需要的朋友可以參考下2018-05-05python 并發(fā)下載器實(shí)現(xiàn)方法示例
這篇文章主要介紹了python 并發(fā)下載器實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了并發(fā)下載器相關(guān)原理及Python并發(fā)下載視頻的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11Python中使用items()方法返回字典元素對(duì)的教程
這篇文章主要介紹了Python中使用items()方法返回字典元素對(duì)的教程,是Python入門(mén)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05Python+radar實(shí)現(xiàn)隨機(jī)日期時(shí)間的生成
Python有廣泛豐富的第三方庫(kù),在沒(méi)有特殊定制下,避免了重復(fù)造輪子。本文將利用radar庫(kù)實(shí)現(xiàn)生成隨機(jī)的日期或時(shí)間,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-05-05Python利用format函數(shù)實(shí)現(xiàn)對(duì)齊打印(左對(duì)齊、右對(duì)齊與居中對(duì)齊)
format是字符串內(nèi)嵌的一個(gè)方法,用于格式化字符串,下面這篇文章主要給大家介紹了關(guān)于Python利用format函數(shù)實(shí)現(xiàn)對(duì)齊打印(左對(duì)齊、右對(duì)齊與居中對(duì)齊)的相關(guān)資料,需要的朋友可以參考下2022-04-04python3.4用循環(huán)往mysql5.7中寫(xiě)數(shù)據(jù)并輸出的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇python3.4用循環(huán)往mysql5.7中寫(xiě)數(shù)據(jù)并輸出的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06新手必備Python開(kāi)發(fā)環(huán)境搭建教程
今天向大家介紹如何搭建Python開(kāi)發(fā)環(huán)境, Python可應(yīng)用于多平臺(tái)包括 Linux 和 Mac OS X,文中有非常詳細(xì)的圖文介紹,需要的朋友可以參考下2021-05-05