Python 使用tkinter和openpyxl导入excel工作表
我想使用浏览按钮导入两个xlsx: 这是我使用的代码:Python 使用tkinter和openpyxl导入excel工作表,python,excel,tkinter,Python,Excel,Tkinter,我想使用浏览按钮导入两个xlsx: 这是我使用的代码: app=Tk() def callback(): chart_path=askopenfilename() return file_location1=Button(app,text="TB v1",width=15, command=callback) file_location1.pack(side='top') file_location2=Button(app,text="TB v2",width=15, co
app=Tk()
def callback():
chart_path=askopenfilename()
return
file_location1=Button(app,text="TB v1",width=15, command=callback)
file_location1.pack(side='top')
file_location2=Button(app,text="TB v2",width=15, command=callback)
file_location2.pack(side='top')
wb1= openpyxl.load_workbook(file_location1)
ws1= wb1.active
wb2= openpyxl.load_workbook(file_location2)
ws2=wb2.active
但是当我构建脚本时,我收到这个错误:TypeError:参数应该是字符串、字节或整数,而不是按钮
有人能帮我吗?问题是您正在传递文件名所在的按钮,请尝试以下操作: 首先导入所有模块
from tkinter import *
from tkinter.filedialog import askopenfile
from openpyxl import load_workbook
创建您的窗口:root = Tk()
root.geometry('200x100')
然后创建您的函数:
def open_file():
file = askopenfile(mode ='r', filetypes =[('Excel Files', '*.xlsx *.xlsm *.sxc *.ods *.csv *.tsv')]) # To open the file that you want.
#' mode='r' ' is to tell the filedialog to read the file
# 'filetypes=[()]' is to filter the files shown as only Excel files
wb = load_workbook(filename = file.name) # Load into openpyxl
wb2 = wb.active
#Whatever you want to do with the WorkSheet
然后是其他一切:
btn = Button(root, text ='Open', command = open_file)
btn.pack(side='top')
mainloop()
欢迎来到堆栈溢出!考虑将代码封装到代码块中,以提高您的问题的可读性以及快速回答的可能性。您正在传递一个按钮到