python快速对比两个excel的数据是否一致

python快速对比两个excel的数据是否一致

    • 功能概述
    • 导入包
    • 封装函数
    • 程序开始

功能概述

两个sheet里面的表头有部分不一致,但是数据对应的内容是一致的,因此需要匹配表格多的值是否一致。
输出两个对比的表格,不一致的地方采用高亮背景色做了区分。
刚开始学习使用python自动化办公,写的不够好可能还存在问题,欢迎交流学习。

导入包

import pandas as pd
#import numpy as np  #有的版本不加numpy不可以用pandas
import openpyxl as vb
import operator as op
import operator
from openpyxl.styles import PatternFill
from openpyxl.styles import colors
from openpyxl.styles import Font

封装函数

# 封装函数1:得到特表列表
def get_p_header_list(table_path,title):
    p_header_list = []
    workbook = vb.load_workbook(table_path)
    sheet_names = workbook.sheetnames    
    for sheet_name in sheet_names:
        my_sheet = workbook[sheet_name]
        sheet_names = workbook.sheetnames
        cell = my_sheet.cell(1,1).value
        if not op.contains(cell,title):
            p_header_list.append(sheet_name)
    return(p_header_list)
 
# 封装函数2:定义header,特殊sheet表取hander
def get_header(sheet_name,p_header_list):
#    my_sheeta = workbook_a[sheet_name]
#    c = my_sheeta.cell(1,1).value
    if sheet_name in p_header_list :
        header = 0     # 三个特殊汇总表
    else :
        header = 1     # 工资表
    return header
 
# 封装函数3:根据表的名字和需要删除的表头的列表,得到所需要的对比的col_lists列表
def get_col_lists(table_path,sheet_name,col_drops=[],p_header_list=[]):
    col_lists = []
#    sheet_name = str(sheet_name)
    workbook = vb.load_workbook(table_path)
    my_sheet = workbook[sheet_name]
    maxcol = my_sheet.max_column     # 最大列
    if sheet_name in p_header_list:
        header = get_header(sheet_name,p_header_list) + 1
        for i in range(header,maxcol+1):
            c = my_sheet.cell(1,i).value  # 特殊表表头  表头在第一行
            if c not in col_drops:  
                if  c != None:
                    col_lists.append(c) 
    else :
        for i in range(1,maxcol+1):
            header = get_header(sheet_name,p_header_list) + 1
            d = my_sheet.cell(header,i).value  # 工资表表头 # 获取表头,默认表头在第二行,并确定表头是否完整,去除不要的列
            if d not in col_drops:  
                if  d != None:
                    col_lists.append(d)
    return(col_lists)
 
# 封装函数4 ,表路径、sheet在工作簿中的位置或者是名字、表头所在位置(第几行)、所需表头字段的列表集合。返回一个df对象
def takecol(table_path,sheet_name,title,col_drops):
    p_header_list = get_p_header_list(table_path,title)
    col_lists = get_col_lists(table_path,sheet_name,col_drops,p_header_list)
    header = get_header(sheet_name,p_header_list)
     
    df = pd.read_excel(table_path,
    sheet_name = sheet_name,# 读取哪⼀个Excel中⼯作表,默认第⼀个
    header = header,# 使⽤第⼀⾏数据作为列索引
    #names = list('ABCDE'),# 替换⾏索引
    #index_col=1
                 )# 指定⾏索引, B作为⾏索引
#    print(sheet_name) #调试用 查看sheet名字
    df1 = df.loc[:,col_lists]
#    df1 = df[col_lists]
    return(df1)
# eg:takecol('table1(20年6月-21年12月)(3.7吴双).xlsx','21年1月','九江12345热线',col_lists)
 
# 封装函数5 ,清洗后的工作簿输出(title剔除、列剔除然后保存,默认新保存文件名已经写死)
def wb_to_excel(table_path,title,col_drops):
    dic = {}
    #1、读取工作表
    workbook = vb.load_workbook(table_path)
    #2、获取原来的所有sheet名字
    sheet_names = workbook.sheetnames
     
    for sheet_name in sheet_names:
        sheet_name = str(sheet_name)
        df = takecol(table_path,sheet_name,title,col_drops)
        dic[sheet_name] = df
    key_ls = [*dic]
    # 获取所有字典的key值列表(一个key对应一个sheetname)
    with pd.ExcelWriter('./diff-' + table_path) as writer:
        for sheet_name in key_ls:
            df = dic[sheet_name]
            df.to_excel(writer,sheet_name=sheet_name,index = False)
 
# 封装函数6 ,对比两个表格的数据 不同的用颜色标记出来
def datas_diff(sheet_a,sheet_b):
    maxrow = sheet_a.max_row
    maxcol = sheet_a.max_column
    for i in range(1,maxrow+1):
        for j in range(1,maxcol+1):
            cell_a = sheet_a.cell(i,j)
            cell_b = sheet_b.cell(i,j)
            if cell_a.value != cell_b.value:
                cell_a.fill = PatternFill('solid',fgColor='FFFF00')
                cell_a.font = Font(color=colors.BLUE,bold=True)
                cell_b.fill = PatternFill('solid',fgColor='FFFF00')
                cell_b.font = Font(color=colors.BLUE,bold=True)    

程序开始

# 代码开始:
# 参数如下:
 
#1预读取工作簿
#workbook_a = vb.load_workbook(r'table1(20年6月-21年12月)(3.7吴双).xlsx')
#workbook_b = vb.load_workbook(r'table2(20年6月-22年3月)(4.28高斐).xlsx')
 
 
#1、清洗table1数据
table_patha = 'table1(20年6月-21年12月)(3.7吴双).xlsx'
title = '九江12345热线' # 不需要的表头,合并单元格只读取cell(1,1)
col_drops = ['序号','公司平台工号'] #不需要的列
wb_to_excel(table_patha,title,col_drops)
 
#2、清洗table2数据
table_pathb = 'table2(20年6月-22年3月)(4.28高斐).xlsx'
title = '九江12345热线' # 不需要的表头,合并单元格只读取cell(1,1)
col_drops = ['序号','公司平台工号'] #不需要的列
wb_to_excel(table_pathb,title,col_drops)
 
#3读取清洗后的工作簿
table_patha_new = './diff-' + table_patha
table_pathb_new = './diff-' + table_pathb
workbook_a = vb.load_workbook(table_patha_new)
workbook_b = vb.load_workbook(table_pathb_new)
 
#4、 开始对比
anames = workbook_a.sheetnames
bnames = workbook_b.sheetnames
 
for aname in anames:
        if True :
            sheet_a = workbook_a[aname]
            sheet_b = workbook_b[aname]
            datas_diff(sheet_a,sheet_b)
        else :
            print('sheet错误')
print('对比完成')   
 
#5、 结果输出  
workbook_a.save('table1_diff_python.xlsx')
workbook_b.save('table2_diff_python.xlsx')

你可能感兴趣的:(python,excel,numpy)