欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)對(duì)比兩個(gè)Excel數(shù)據(jù)內(nèi)容并標(biāo)記出不同

 更新時(shí)間:2022年12月26日 15:16:41   作者:Sir 老王  
日常工作中需要對(duì)比兩個(gè)Excel工作表中的數(shù)據(jù)差異是很不方便的,使用python來(lái)做就比較簡(jiǎn)單了!本文為大家介紹了python實(shí)現(xiàn)對(duì)比兩個(gè)Excel的數(shù)據(jù)內(nèi)容并標(biāo)記出不同數(shù)據(jù)的示例代碼,需要的可以參考一下

日常工作中需要對(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)文章

最新評(píng)論