我编写了一个根据自定义位置获取excel数据的程序,代码可以正常运行,正常运行代码如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#导入模块
import os
import pandas as pd
from pandas import DataFrame
#创建excel文件目录列表
excel_names=[]
for excel_name in os.listdir('/Users/Administrator/Desktop/ns'):
excel_names.append(excel_name)
my_list = list(range(148))
#创建空列表
checkcell=[]
#创建空字典
checkcell_C={}
#定位excel文件中单元格坐标,x为行坐标,y为列坐标
excel_path=f'/Users/Administrator/Desktop/dw.xlsx'#先将目标单元格坐标放入02文件
df=pd.read_excel(excel_path)
x=df.values[:,0]#读取x坐标
y=df.values[:,1]#读取有坐标
#读取excel目标单元格
for excel_name in excel_names:
excel_path=f'/Users/Administrator/Desktop/ns/{excel_name}'#循环读取文件夹中excel文件
df_splist=pd.read_excel(excel_path,sheet_name=0)#循环读取excel文件中工作表
cell=df_splist.values[x,y]#读取目标单元格数据
checkcell.append(cell)#单元格数据导入列表数组
checkcell_C=dict(zip(my_list,checkcell))#数组数据导入字典
#输出目标文件
data=DataFrame(checkcell_C)#数据化字典
DataFrame(data).to_excel(r'C:\Users\Administrator\Desktop\cs1.xlsx',sheet_name='测试',index=False)#输出文件
但是当我将上述程序窗口化过程中,我遇到了以下问题
#导入模块
import tkinter as tk
from tkinter import filedialog
import os
import pandas as pd
from pandas import DataFrame
def selectfolderpath():
foldername=filedialog.askdirectory()
path1.set(foldername)
def select_locationexcelpath():
locationname=filedialog.askopenfilename()
path2.set(locationname)
#创建窗口
window=tk.Tk(className="excel指定单元格数据读取程序")
#设定窗口大小(长*宽)
window.geometry("500x500")
path1=tk.StringVar()
path2=tk.StringVar()
#创建横纵坐标输入框及及框位置
btn_folder=tk.Button(window,text="选择读取文件夹",width=12,command=selectfolderpath)
btn_folder.place(x=5,y=110)
folder_path=tk.Entry(window,textvariable=path1,width=45)
folder_path.place(x=120,y=110)
btn_locationexcel=tk.Button(window,text="选择定位文件",width=12,command=select_locationexcelpath)
btn_locationexcel.place(x=5,y=150)
locationexcel_path=tk.Entry(window,textvariable=path2,width=45)
locationexcel_path.place(x=120,y=150)
sheetname1=tk.Label(window,text="输入工作表名称")
sheetname1.place(x=5,y=190)
sheetname_path=tk.Entry(window,width=45)
sheetname_path.place(x=120,y=190)
def getdata():
#创建excel文件目录列表
excel_names=[]
for excel_name in os.listdir(path1.get()):
excel_names.append(excel_name)
my_list = list(range(200))
#创建空列表
checkcell=[]
#创建空字典
checkcell_C={}
#定位excel文件中单元格坐标,x为行坐标,y为列坐标
location_path=path2.get()#先将目标单元格坐标放入02文件
df=pd.read_excel(location_path)
x1=(df.iloc[:,0])#读取x坐标
x2=[x1 for x1 in x1 if x1 == x1]
x=[int(a) for a in x2]
print(x)
y1=(df.iloc[:,1])#读取y坐标
y2=[y1 for y1 in y1 if y1 == y1]
y=[int(b) for b in y2]
print(y)
#读取目标单元格数据
for excel_name in excel_names:
excel_path=path1.get()+f'/'+f'{excel_name}'#循环读取文件夹中excel文件
df_splist=pd.read_excel(excel_path,sheet_name=int(sheetname_path.get())-1)#循环读取exacel文件中工作表
cell=df_splist.values[(x),(y)]#读取目标单元格数据
checkcell.append(cell)#单元格数据导入列表数组
checkcell_C=dict(zip(my_list,checkcell))
#数组数据导入字典
#输出目标文件
data=DataFrame(checkcell_C)#数据化字典
DataFrame(data).to_excel(str(path1.get())+'/data.xlsx',sheet_name='数据汇总',index=False)#输出文件
star=tk.Button(window,text="开始",width=10,command=getdata)
star.place(x=250,y=250)
window.mainloop()
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "<stdin>", line 19, in getdata
IndexError: index 64 is out of bounds for axis 0 with size 64
即 cell=df_splist.values[(x),(y)]#读取目标单元格数据该行一直报错,请帮帮我到底是什么原因?不胜感激。
数组索引越界,大小为64个的元素,索引只能是0-63,检查获取的x,y值,并查看是否有的文件行列数不一致。