python小程序,提取路径下所有excel表中数据,输入界面是基于TK语言编写的
用python识别路径下所有excel文件,并检索每个excel文件中的sheet表,并匹配搜索关键词,如果sheet表中含有关键词则提取表中的markov数据到数组中,同时点击数据输出可将数据输出到名称为output_data_liu的excel表中。
from tkinter import Tk, Button, filedialog, Label, Entry
# 创建主窗口
window = Tk()
##################################### 创建3个静态文本框和输入框定义矩阵规模和数量
label1 = Label(window, text="矩阵行数")
label1.grid(row=0, column=0)
entry1 = Entry(window)
entry1.grid(row=0, column=1)
label2 = Label(window, text="矩阵列数")
label2.grid(row=1, column=0)
entry2 = Entry(window)
entry2.grid(row=1, column=1)
label3 = Label(window, text="矩阵数量")
label3.grid(row=2, column=0)
entry3 = Entry(window)
entry3.grid(row=2, column=1)
label4 = Label(window, text="载荷类型_表格sheet关键词")
label4.grid(row=0, column=2)
entry4 = Entry(window)
entry4.grid(row=0, column=3)
###########################################定义矩阵初始化子函数,根据静态文本框数据生成一定数量一定格式的矩阵
def martrix_gen():
entry1.delete(0, 'end') # 设置缺省值128
entry1.insert('end', 128)
entry2.delete(0, 'end') # 设置缺省值128
entry2.insert('end', 128)
entry3.delete(0, 'end') # 设置缺省值
entry3.insert('end', int(len(filepath)))
global input_data0
global input_data1
global input_data2
input_data0 = int(entry1.get())+1
input_data1 = int(entry2.get())+1
input_data2 = int(entry3.get())
print(input_data0 )
print(input_data1 )
print(input_data2 )
num_arrays = input_data2 # 要创建的二维数组的数量
array_size0 = input_data0 # 矩阵行数
array_size1 = input_data1 # 矩阵列数
global arrays
arrays = [] # 存储多个二维数组的列表
# 使用 for 循创建多个矩阵
for _ in range(num_arrays):
array = [[0] * input_data1 for _ in range(input_data0 )]
arrays.append(array)
for i, array in enumerate(arrays, start=1):
print(f"Array {i}:")
for row in array:
print(row)
print()
###########################################定义选择文件子函数
def open_file():
Tk().withdraw() # 隐藏根窗口
global file_names
global folder_path
global filepath
folder_path = filedialog.askdirectory()
print(folder_path)
import os
file_names = [file for file in os.listdir(folder_path) if file.endswith('.xls') or file.endswith('.xlsx')]
print(file_names[0])
print(len(file_names))
filepath= [[[] for i in range(1)] for i in range(len(file_names))]
###########################################将路径名称与文件名称合并为后续数据读取做准备
for i in range(len(file_names)):
filepath[i][0] = os.path.join(folder_path, file_names[i])
print(filepath)
for row_1 in filepath:
print(row_1)
print(len(filepath))
###########################################定义装载数据子函数
def data_load():
Tk().withdraw() # 隐藏根窗口
print(len(filepath))
print(arrays)
import pandas as pd
global keyword
keyword = str(entry4.get())
for i in range(input_data2):
xls = pd.read_excel(filepath[i][0], sheet_name=None)
for sheet_name, df in xls.items():
if keyword in sheet_name:
data_temp = pd.read_excel(filepath[i][0], sheet_name, header=None).iloc[3:132, 0:129]
print(sheet_name)
for j in range(input_data0):
for k in range(input_data1):
arrays[i][j][k]=data_temp[k+0][3+j]
print(data_temp)
data1=arrays[1][15][0]*2
print(data1)
print(arrays[1][15][1])
print(arrays[4][15][1])
import tkinter.messagebox
tkinter.messagebox.showinfo('数据装载完成')
###########################################定义输出数据子函数
def data_output():
Tk().withdraw() # 隐藏根窗口
global folder_path2
global filemane_temp1
import os
from openpyxl import Workbook
from openpyxl import load_workbook
import pandas as pd
# 在指定路径下生成名为outputdata的excel文件
filemane_temp1='output_data_liu.xlsx'
folder_path2 = filedialog.askdirectory()
folder_path2 = os.path.join(folder_path2, filemane_temp1)
workbook = Workbook()
workbook.save(folder_path2)
workbook = load_workbook(folder_path2)
# 在指定路径下生成名为outputdata的excel文件生成新sheet
for i in range(input_data2):
xls = pd.read_excel(filepath[i][0], sheet_name=None)
for sheet_name, df in xls.items():
if keyword in sheet_name:
new_sheet = workbook.create_sheet(title=file_names[i])
data_temp = pd.read_excel(filepath[i][0], sheet_name, header=None).iloc[3:132, 0:129]
for j in range(input_data0):
for k in range(input_data1):
new_sheet.cell(row=j + 1, column=k + 1).value = data_temp[k + 0][3 + j]
workbook.save(folder_path2)
import tkinter.messagebox
tkinter.messagebox.showinfo('数据输出完成')
############################################## 矩阵初始化按钮
button = Button(window, text="矩阵初始化", command=martrix_gen)
button.grid(row=5, column=1)
############################################## 创建文件选择按钮
button = Button(window, text="选择文件", command=open_file)
button.grid(row=4, column=1)
############################################## 创建装载数据按钮
button = Button(window, text="数据装载", command=data_load)
button.grid(row=1, column=3)
############################################## 创建输出数据按钮
button = Button(window, text="数据输出", command=data_output)
button.grid(row=2, column=3)
##################################################### 运行主循环
window.mainloop()