使用Python解决对比出两个Excel文件中的不同项并将结果重新写入一个新的Excel文件

因为有统计成员到会情况的任务,每次汇总时都很麻烦,需要一个个对应腾讯会议导出名单的成员,然后在总表上进行标记,所以就写了本程序来减少统计的复杂度。

使用xlrdxlwt
首先安装两个包

pip install xlrd == 1.2.0
pip install xlwt == 0.7.5

定义contrast函数

def contrast(processed_export_excel_file,all_number_file,different_name_file):"""@param processed_export_excel_file:处理后的导出名单@param all_number_file:总人数的名单@param different_name_file:导出文件的地址"""# 打开Excel文件# 打开处理后的导出名单data1 = xlrd.open_workbook(processed_export_excel_file)# 打开总人数的名单data2 = xlrd.open_workbook(all_number_file)# 获取第一个sheetsheet1 = data1.sheet_by_index(0)sheet2 = data2.sheet_by_index(0)#  获取两个Excel文件的行数和列数grows1 = sheet1.nrowsgrows2 = sheet2.nrows# 创建一个新的Excel文件new_excel = xlwt.Workbook()new_sheet = new_excel.add_sheet('未参会人员')# 相同项same_content = []# sheet2中的所有人员excel_2_content = []# 未参会人员diff_content = []for i in range(grows2):excel_2_content.append(sheet2.cell_value(i, 0))for i in range(grows1):for j in range(grows2):sheet1_value = sheet1.cell_value(i, 0)sheet2_value = sheet2.cell_value(j, 0)# sheet1的字符串包含sheet2的字符串if str(sheet2_value) in str(sheet1_value):same_content.append(sheet2_value)# 找出excel_2_content中不在same_content中的内容for i in excel_2_content:if i not in same_content:diff_content.append(i)print("原有内容:", excel_2_content)print("相同项:" + str(same_content))print("不同项:" + str(diff_content))print("总共有" + str(len(diff_content)) + "个不同项")# 将不同项写入新的Excel文件for i in range(len(diff_content)):new_sheet.write(i, 0, diff_content[i])new_excel.save(different_name_file)

测试contrast函数

if __name__ == '__main__':file1 = r"C:/Users/MSI/Desktop/test1.xlsx"file2 = r"C:/Users/MSI/Desktop/test2.xlsx"outfile = r"C:/Users/MSI/Desktop/diff.xlsx"contrast(file1, file2, outfile)

完整代码:

import xlrd
import xlwt"""
pip3 install xlrd == 1.2.0
pip3 install xlwt == 0.7.5
"""def contrast(processed_export_excel_file, all_number_file, different_name_file):"""@param processed_export_excel_file: 导出名单处理后@param all_number_file: 总人数的名单@param different_name_file: 导出文件名"""# 打开Excel文件# 打开处理后的导出名单data1 = xlrd.open_workbook(processed_export_excel_file)# 打开总人数的名单data2 = xlrd.open_workbook(all_number_file)# 获取第一个sheetsheet1 = data1.sheet_by_index(0)sheet2 = data2.sheet_by_index(0)#  获取两个Excel文件的行数和列数grows1 = sheet1.nrowsgrows2 = sheet2.nrows# 创建一个新的Excel文件new_excel = xlwt.Workbook()new_sheet = new_excel.add_sheet('未参会人员')# 相同项same_content = []# sheet2中的所有人员excel_2_content = []# 未参会人员diff_content = []for i in range(grows2):excel_2_content.append(sheet2.cell_value(i, 0))for i in range(grows1):for j in range(grows2):sheet1_value = sheet1.cell_value(i, 0)sheet2_value = sheet2.cell_value(j, 0)# sheet1的字符串包含sheet2的字符串if str(sheet2_value) in str(sheet1_value):same_content.append(sheet2_value)# 找出excel_2_content中不在same_content中的内容for i in excel_2_content:if i not in same_content:diff_content.append(i)print("原有内容:", excel_2_content)print("相同项:" + str(same_content))print("不同项:" + str(diff_content))print("总共有" + str(len(diff_content)) + "个不同项")# 将不同项写入新的Excel文件for i in range(len(diff_content)):new_sheet.write(i, 0, diff_content[i])new_excel.save(different_name_file)if __name__ == '__main__':file1 = r"C:/Users/MSI/Desktop/test1.xlsx"file2 = r"C:/Users/MSI/Desktop/test2.xlsx"outfile = r"C:/Users/MSI/Desktop/diff.xlsx"contrast(file1, file2, outfile)

到这里,核心功能已经做出来了。但还不是很方便,每次都需要打开程序,重新输入Excel所在的路径。那就再加上一点细节,做个界面,把程序打包成exe文件吧。
下面是详细步骤:

安装tkinter包
pip install tkinter == 8.6.7

导入tkinter

import tkinter as tk
import tkinter.filedialog
from tkinter import *
from tkinter import messagebox

写个函数用来选择路径

def select_export_path():temp = tk.filedialog.askopenfilename()export_path.set(temp)def select_all_number_path():temp = tk.filedialog.askopenfilename()all_number_Path.set(temp)def select_diff_path():temp = tk.filedialog.askopenfilename()diff_path.set(temp)

初始化变量

root = tk.Tk()
export_path = StringVar()
all_number_Path = StringVar()
diff_path = StringVar()

画出UI界面

def ui():"""选择界面设计以及路径功能"""root.title("对比工具")root.geometry("400x200")# 标签tk.Label(root, text="导出参会成员名单:").grid(row=0, column=0)tk.Label(root, text="全部成员名单:").grid(row=1, column=0)tk.Label(root, text="未参会成员名单:").grid(row=2, column=0)# 输入框processed_export_excel_file = tk.Entry(root, textvariable=export_path)processed_export_excel_file.grid(row=0, column=1)all_number_file = tk.Entry(root, textvariable=all_number_Path)all_number_file.grid(row=1, column=1)different_name_file = tk.Entry(root, textvariable=diff_path)different_name_file.grid(row=2, column=1)# 按钮tk.Button(root, text="选择文件", command=select_export_path).grid(row=0, column=2)tk.Button(root, text="选择文件", command=select_all_number_path).grid(row=1, column=2)tk.Button(root, text="选择文件", command=select_diff_path).grid(row=2, column=2)tk.Button(root, text="开始对比",command=lambda: contrast_button_clicked(processed_export_excel_file.get(), all_number_file.get(),different_name_file.get())).grid(row=3, column=1)root.mainloop()

点击对比按钮后的函数

def contrast_button_clicked(processed_export_excel_file, all_number_file, different_name_file):contrast_result, number = contrast(processed_export_excel_file, all_number_file, different_name_file)if contrast_result:tk.messagebox.showinfo("提示", "对比成功!共有"+str(number)+"人缺会,详细情况请到" + different_name_file + "文件查看")else:tk.messagebox.showinfo("提示", "对比失败!请检查输入路径是否正确")

看一下效果吧

接下来就是把这个py程序打包,使用pyinstaller这个包
pip install pyinstaller
安装成功之后,按键盘win+R打开运行,输入cmd,回车运行。
进入程序所在文件夹:

然后输入pyinstaller -F -w contrast.py --hidden-import=pandas._libs.tslibs.timedeltas

成功之后便可在程序根目录dist文件夹里边便可看到封装好的exe文件。

完整代码如下:

import xlrd
import xlwt
import tkinter as tk
import tkinter.filedialog
from tkinter import *
from tkinter import messagebox"""
pip3 install xlrd == 1.2.0
pip3 install xlwt == 0.7.5
pip3 install tkinter == 8.6.7
"""def select_export_path():temp = tk.filedialog.askopenfilename()export_path.set(temp)def select_all_number_path():temp = tk.filedialog.askopenfilename()all_number_Path.set(temp)def select_diff_path():temp = tk.filedialog.askopenfilename()diff_path.set(temp)root = tk.Tk()
export_path = StringVar()
all_number_Path = StringVar()
diff_path = StringVar()def ui():"""选择界面设计以及路径功能"""root.title("对比工具")root.geometry("400x200")# 标签tk.Label(root, text="导出参会成员名单:").grid(row=0, column=0)tk.Label(root, text="全部成员名单:").grid(row=1, column=0)tk.Label(root, text="未参会成员名单:").grid(row=2, column=0)# 输入框processed_export_excel_file = tk.Entry(root, textvariable=export_path)processed_export_excel_file.grid(row=0, column=1)all_number_file = tk.Entry(root, textvariable=all_number_Path)all_number_file.grid(row=1, column=1)different_name_file = tk.Entry(root, textvariable=diff_path)different_name_file.grid(row=2, column=1)# 按钮tk.Button(root, text="选择文件", command=select_export_path).grid(row=0, column=2)tk.Button(root, text="选择文件", command=select_all_number_path).grid(row=1, column=2)tk.Button(root, text="选择文件", command=select_diff_path).grid(row=2, column=2)tk.Button(root, text="开始对比",command=lambda: contrast_button_clicked(processed_export_excel_file.get(), all_number_file.get(),different_name_file.get())).grid(row=3, column=1)root.mainloop()def contrast_button_clicked(processed_export_excel_file, all_number_file, different_name_file):contrast_result, number = contrast(processed_export_excel_file, all_number_file, different_name_file)if contrast_result:tk.messagebox.showinfo("提示", "对比成功!共有"+str(number)+"人缺会,详细情况请到" + different_name_file + "文件查看")else:tk.messagebox.showinfo("提示", "对比失败!请检查输入路径是否正确")def contrast(processed_export_excel_file, all_number_file, different_name_file):"""@param processed_export_excel_file: 导出名单处理后@param all_number_file: 总人数的名单@param different_name_file: 导出文件名"""# 打开Excel文件# 打开处理后的导出名单global resultdata1 = xlrd.open_workbook(processed_export_excel_file)# 打开总人数的名单data2 = xlrd.open_workbook(all_number_file)# 获取第一个sheetsheet1 = data1.sheet_by_index(0)sheet2 = data2.sheet_by_index(0)#  获取两个Excel文件的行数和列数grows1 = sheet1.nrowsgrows2 = sheet2.nrows# 创建一个新的Excel文件new_excel = xlwt.Workbook()new_sheet = new_excel.add_sheet('未参会人员')# 相同项same_content = []# sheet2中的所有人员excel_2_content = []# 未参会人员diff_content = []for i in range(grows2):excel_2_content.append(sheet2.cell_value(i, 0))for i in range(grows1):for j in range(grows2):sheet1_value = sheet1.cell_value(i, 0)sheet2_value = sheet2.cell_value(j, 0)# sheet1的字符串包含sheet2的字符串if str(sheet2_value) in str(sheet1_value):same_content.append(sheet2_value)# 找出excel_2_content中不在same_content中的内容for i in excel_2_content:if i not in same_content:diff_content.append(i)print("原有内容:", excel_2_content)print("相同项:" + str(same_content))print("不同项:" + str(diff_content))print("总共有" + str(len(diff_content)) + "个不同项")# 将不同项写入新的Excel文件for i in range(len(diff_content)):new_sheet.write(i, 0, diff_content[i])result = Truenew_excel.save(different_name_file)return result, len(diff_content)if __name__ == '__main__':ui()

使用Python解决对比出两个Excel文件中的不同项并将结果重新写入一个新的Excel文件相关推荐

  1. 如何利用python将excel表格中筛选出来的每一份数据各自另存为新的excel文件?

    如何利用python将excel表格中筛选出来的每一份数据各自另存为新的excel文件? 1.问题描述 2.解决过程 2.1 问题分析: 2.2 解决思路 3.运行结果 1.问题描述 最近在处理一堆工 ...

  2. python处理两个Excel并且根据相同字段合并写入一个新的Excel

    在做数据处理的工作中,经常会遇见多个Excel文件,然后内容不同,有关联字段,需要将其合并在一起,然后生成一个新的文件放在一个新的excel里. # 1.首先我们需要封装两个类 ①一个类是专门读取Ex ...

  3. python处理微信消息导入excel_使用python读取excel中的数据,并重新写入到新的excel中...

    使用Python3运行 起初是读取excel中的数据,进行相关的计算将结果追加在后面,没有找到合适的办法,就只能采用这种读取数据,进行相关操作,重新写入的方法 1. 主要流程 (1)使用xlrd打开x ...

  4. python对excel两列求和写入另一列_python读取excel指定列数据并写入到新的excel方法...

    如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据------------------------- ...

  5. python 在excel指定列添加数据_python读取excel指定列数据并写入到新的excel方法

    如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据------------------------- ...

  6. python excel写入一列_python读取excel指定列数据并写入到新的excel方法

    如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据------------------------- ...

  7. 成功解决在excel表格中仅在某列内插入一个空白单元格,使其下部的数据整体下移一格

    成功解决在excel表格中仅在某列内插入一个空白单元格,使其下部的数据整体下移一格 目录 解决问题 解决方法 解决问题 在excel表格中仅在某列内插入一个空白单元格,使其下部的数据整体下移一格 解决 ...

  8. C语言socket accept()函数(提取出所监听套接字的等待连接队列中第一个连接请求,创建一个新的套接字,并返回指向该套接字的文件描述符)

    文章目录 名称 使用格式 功能参数描述 参数 sockfd addr addrlen 返回值 示例 man 2 文档中的accept解释 错误处理 名称 accept() 接收一个套接字中已建立的连接 ...

  9. LeetCode4.python实现:寻找两个有序数组中的中位数问题☆☆☆

    目录 问题 解题思路 python具体实现 题外话 问题 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log( ...

最新文章

  1. python shell背景颜色改变_科学网—Python Shell Background Color - 李旭的博文
  2. GIS实战应用案例100篇(一)-GEE主成分分析(含代码)
  3. c语言递归建链表,递归创建二叉树c语言实现+详细解释
  4. mac ssh客户端_Electerm for Mac(ssh客户端)
  5. php进阶课程,php进阶教程学习
  6. 表格状态列_不用软件也能做好多个项目跟进管理?我用一个协同表格就搞定
  7. Hashmap扩容时出现循环链表(jdk1.8把头插法换成了尾插法的原因)
  8. Windows Azure Cloud Service (41) 修改云服务IIS托管管道模式为4.0经典模式
  9. exit、break、continue的区别
  10. MySQL特有的SQL语句 第一弹
  11. Nexus3 私服搭建和配置
  12. 达梦数据库html管理,达梦数据库的管理 - osc_nbqoh20k的个人空间 - OSCHINA - 中文开源技术交流社区...
  13. 按出生年月日对身份证号进行排序
  14. 如何获取篮球比赛即时赔率
  15. 富文本编辑器mavon-editor文章回显
  16. javaScript高级程序设计-------总结随笔
  17. AMESim锂离子电池包电化学机理模型
  18. 【论文翻译笔记】Test Roll: Profit-Maximizing A/B Tests
  19. php 感叹号有什么用,感叹号!代表什么意思?(标点符号的用法之感叹号)
  20. button按钮样式

热门文章

  1. ccd无法连接到计算机,激光三维手持式扫描仪无法连接电脑维修技术人员多
  2. 【学习笔记】大数据技术之Scala(下)
  3. VS 2019 专业版离线安装
  4. (HTM)时间沉积池算法细节-翻译
  5. xftp安装包_Vos3000 6.0、vos3000 7.0安装教程和安装包
  6. 【打猫创新-设备研究】涉诈远控APK阳桃控专项分析
  7. 《被讨厌的勇气》——第三夜阅读摘记
  8. 湖北技能高考计算机本科学校有哪些,湖北技能高考能进哪些学校
  9. wmts格式说明_OpenLayers教程:多源数据加载之WMTS
  10. wmts规范_【总结整理】WMS、WMTS、WFS