时间:2022-07-10 09:40:05 | 栏目:Python代码 | 点击:次
目的:设计一个应用GUI用于对比两个Excel文件
思路
(默认新旧文件列名一致)
配置
import pandas as pd from openpyxl import load_workbook #选择文件路径 path=r"C:\Users\小管同学\Desktop\Migration_Data_Compari\对比文件.xls" #input("选择文件路径:") TargetPath=r"C:\Users\小管同学\Desktop\Migration_Data_Comparison_Tool\目标文件\对比结果.xlsx" DATA_ODS=pd.read_excel(r"C:\Users\小管同学\Desktop\Migration_Data_Comparison_Tool\对比文件.xls",sheet_name="ODS") DATA_DWH=pd.read_excel(r"C:\Users\小管同学\Desktop\Migration_Data_Comparison_Tool\对比文件.xls",sheet_name="DWH") #选择主键 Primarykey="员工编号"#input("选择主键1:") Primarykey # 员工编号
一、数据量
输出表格1–数据量
def write_to_excel_DataVolume(Data,TargetPath): # cor_df 为要保存的 dataframe writer = pd.ExcelWriter(TargetPath, engine='xlsxwriter') # 这里用 Data.to_excel(writer,sheet_name='Sheet1', encoding='utf8', header=False, startcol=0, startrow=2) # 把dataframe的数据从第2行开始 workbook = writer.book format1 = workbook.add_format({ # 先把样式打包,然后之后赋值即可 'bold': True, # 字体加粗 'text_wrap': True, # 是否自动换行 'valign': 'bottom', #垂直对齐方式 'align': 'center', # 水平对齐方式 'fg_color': '#C5D9F1', # 单元格背景颜色 'border': 1,# 边框 }) writer_sheet = writer.sheets['Sheet1'] # 设置宽度 writer_sheet.set_column("A:I", 16) writer_sheet.set_column('C:C',30) writer_sheet.merge_range(0,0,0,2,'对比结果',format1) writer_sheet.merge_range(4,2,4,0,'数据量差异',format1) writer_sheet.write(1,0,'',format1) writer_sheet.write(1,1,'ODS',format1) writer_sheet.write(1,2,'DWH',format1) writer.save() writer.close() DataFrame_DataVolume=pd.DataFrame([[DATA_ODS.shape[0]],[DATA_DWH.shape[0]]]).T DataFrame_DataVolume.columns =["ODS","DWH"] DataFrame_DataVolume.index=["数据量"] DataFrame_DataVolume #writeFileDataVolume(DataFrame_DataVolume,TargetPath) write_to_excel_DataVolume(DataFrame_DataVolume,TargetPath)
输出表格2–数据量差异合同
if DATA_ODS.shape[0]==DATA_DWH.shape[0]: pass else: DATA_ODS_Primarykey=pd.DataFrame(DATA_ODS[Primarykey]) DATA_DWH_Primarykey=pd.DataFrame(DATA_DWH[Primarykey]) df_union = pd.concat([DATA_ODS_Primarykey,DATA_DWH_Primarykey]) # 实现1 df_diff_ODS = df_union.append(DATA_ODS_Primarykey).drop_duplicates(subset=df_union.columns.to_list(), keep=False) df_diff_DWH = df_union.append(DATA_DWH_Primarykey).drop_duplicates(subset=df_union.columns.to_list(), keep=False) #DWH多的合同 df_diff_ODS #DWH少的合同 df_diff_DWH df_diff_DWH_Data=[] df_diff_ODS_Data=[] for i in df_diff_ODS.head(10).values.tolist(): for n in i: df_diff_ODS_Data.append(n) for i in df_diff_DWH.head(10).values.tolist(): df_diff_DWH_Data.append(n) while True: if len(df_diff_DWH_Data)>len(df_diff_ODS_Data): df_diff_ODS_Data.append("-") elif len(df_diff_DWH_Data)< len(df_diff_ODS_Data): df_diff_DWH_Data.append("-") elif len(df_diff_DWH_Data)== len(df_diff_ODS_Data): break DataFrame_DataVolume_Count_result=pd.DataFrame(df_diff_DWH_Data,df_diff_ODS_Data).reset_index() DataFrame_DataVolume_Count_result.columns=['DWH多的合同','DWH少的的合同'] DataFrame_DataVolume_Count_result=DataFrame_DataVolume_Count_result.reset_index() DataFrame_DataVolume_Count_result.columns=['序号','DWH多的合同','DWH少的的合同'] DataFrame_DataVolume_Count_result from openpyxl import load_workbook def write_to_excel_Count_result(Data,TargetPath): df_Old = pd.DataFrame(pd.read_excel(TargetPath)) #读取原数据文件和表 writer = pd.ExcelWriter(TargetPath,engine='openpyxl') book=load_workbook(TargetPath) writer.book = book writer.sheets = dict((ws.title, ws) for ws in book.worksheets) df_rows = df_Old.shape[0] #获取原数据的行数 Data.to_excel(writer,startrow=df_rows+1, index=False,startcol=0,header=True)#将数据写入excel中的aa表,从第一个空行开始写 writer.save()#保存 write_to_excel_Count_result(DataFrame_DataVolume_Count_result,TargetPath)