之前写的一篇文章:利用Python将vlookup自动化,将写的一个脚本打包成了可执行exe文件。
过程演示.gif这次,利用tkinter模块制作GUI界面变成如下:
GUI界面.gif
#!/usr/bin/python
# -*- coding:utf-8 -*-
from tkinter import *
import pandas as pd
import os
import re
window = Tk()
window.title('数据匹配')
window.geometry('300x400')
var1 = StringVar()
var2 = StringVar()
var3 = StringVar()
var4 = StringVar()
var5 = StringVar()
var6 = StringVar()
var7 = StringVar()
L1 = Label( text="请输入表A的名称:")
L1.pack()
e1 = Entry(window,show=None,textvariable=var1)
e1.pack()
L2 = Label( text="请输入表A中sheet名称:")
L2.pack()
e2 = Entry(window,show=None,textvariable=var2)
e2.pack()
L3 = Label( text="请输入表A中的ID列:")
L3.pack()
e3 = Entry(window,show=None,textvariable=var3)
e3.pack()
L4 = Label( text="请输入表B的名称:")
L4.pack()
e4 = Entry(window,show=None,textvariable=var4)
e4.pack()
L5 = Label( text="请输入表B中sheet名称:")
L5.pack()
e5 = Entry(window,show=None,textvariable=var5)
e5.pack()
L6 = Label( text="请输入表B中的ID列:")
L6.pack()
e6 = Entry(window,show=None,textvariable=var6)
e6.pack()
L7 = Label( text="请输入要匹配的表B中的列名:")
L7.pack()
e7 = Entry(window,show=None,textvariable=var7)
e7.pack()
def data_match():
path = 'C:\\Users\\PS20190225\\Desktop'
os.chdir(path)
table_a_path = e1.get()
a_type = re.search(r'([a-z]*).([a-z]*)',table_a_path).group(2)
sheet_a_name = e2.get()
A_id = e3.get()
if a_type == 'xlsx':
table_a = pd.read_excel(table_a_path,sheet_name = sheet_a_name,converters={A_id:str}).dropna(axis=1,how='all')
else:
table_a = pd.read_csv(table_a_path, sheet_name=sheet_a_name, converters={A_id: str}).dropna(axis=1, how='all')
table_b_path = e4.get()
b_type = re.search(r'([a-z]*).([a-z]*)',table_b_path).group(2)
sheet_b_name = e5.get()
B_id = e6.get()
target_col = e7.get()
if b_type == 'xlsx':
table_b = pd.read_excel(table_b_path, sheet_name=sheet_b_name, converters={B_id: str}).dropna(axis=1, how='all')
else:
table_b = pd.read_csv(table_b_path, sheet_name=sheet_b_name, converters={B_id: str}).dropna(axis=1, how='all')
table_b_2 = table_b.groupby(B_id)[target_col].sum().reset_index()
table_c = table_a.merge(right=table_b_2,how='left',left_on=A_id,right_on=B_id)
table_c.to_excel('c.xlsx',index=False)
b = Button(window,text='进行匹配',width=15,height=2,command=data_match)
b.pack()
window.mainloop()
代码详见我的Github:利用tkinter制作GUI窗口
学习tkinter可观看莫凡大佬的视频:做简单窗口视窗
网友评论