Python+Excel筛选未提交人员

起因

学校给了表格让我们班长统计信息,可以用腾讯大大的TIM协作办公,让大家自己填,感觉方便了很多。然而,信息一旦变多而且顺序又没有固定,到后期想要知道未填信息的同学,这就难受了——只能找到班级名单,一个一个对照着找出未提交人员。日后此类事情肯定不会少,作为懒人的我可不想一次次地对照。那这类重复性的工作就交给程序来完成吧。

目标

1.主要目标:对照源文件(班级完整名单)找出目标文件中的未提交人员

2.次要目标:为了程序的的适用性,可以自己选择两个文件,再选择对比列

实现

思考之后,这个问题很简单,其实只需要“两个集合做差集”便可以得到,简单来说如下所示:

 

程序

import xlrd
import tkinter as tk
from tkinter import filedialog,ttk

root = tk.Tk()
root.withdraw()

'''获取源文件与目标文件'''
sourcefile_path = filedialog.askopenfilename(title = 'Please choose contrastive source file')#使用文件选择对话框获取源文件路径
source_book = xlrd.open_workbook(sourcefile_path)#xlrd打开路径指向的Excel文件
source_sheet = source_book.sheet_by_index(0)#默认打开第一个表

targetfile_path = filedialog.askopenfilename(title = 'Please choose contrastive target file')
target_book = xlrd.open_workbook(targetfile_path)
target_sheet = target_book.sheet_by_index(0)

source_tip = source_sheet.row_values(0)#获取源文件第一行的表头信息
target_tip = target_sheet.row_values(0)


'''创建对比项选择窗口'''
contrastive_item = tk.Tk()
contrastive_item.title('Choose contrastive items')

#标签
ttk.Label(contrastive_item, text="Choose source item").grid(column=0, row=0)
ttk.Label(contrastive_item, text="Choose target item").grid(column=1, row=0)

#下拉列表1(包含源文件表头信息)
source_item = ttk.Combobox(contrastive_item, width=20)#创建下拉列表1并设置宽度
source_item['values'] = source_tip#将源文件表头信息放入下拉列表1
source_item.grid(column=0, row=1)#按网状分布放与2行1列
source_item.current(0)#默认选项为首项

#下拉列表2(包含目标文件表头信息)
target_item = ttk.Combobox(contrastive_item, width=20)#创建下拉列表2并设置宽度
target_item['values'] = target_tip#将目标文件表头信息放入下拉列表1
target_item.grid(column=1, row=1)#按网状分布放与2行2列
target_item.current(0)#默认选项为首项

def Confirm():
     source_list = source_sheet.col_values(source_tip.index(source_item.get()))[1:]#读取源文件,所选列除表头外的信息
     target_list = target_sheet.col_values(target_tip.index(target_item.get()))[1:]#读取目标文件,所选列除表头外的信息
     
     diff = list(set(source_list) - set(target_list))#List强转为Set,做差集,再转为list

     '''显示结果'''
     result_root = tk.Tk()
     result = tk.Listbox(result_root)
     for item in diff:
          result.insert(0,item)
     result.pack()
     result_root.mainloop()
          
     
#按钮
action = ttk.Button(contrastive_item, text="Confirm", command=Confirm) #创建按钮,并设置响应为Confirm
action.grid(column=2, row=1)#按网状分布放与2行3列

contrastive_item.mainloop()

重点

最重要的核心代码只有一句:

diff = list(set(source_list) - set(target_list))

测试图

结果:

 

猜你喜欢

转载自blog.csdn.net/qq_41168765/article/details/104305123
今日推荐