Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tkinter和openpyxl导入excel工作表_Python_Excel_Tkinter - Fatal编程技术网

Python 使用tkinter和openpyxl导入excel工作表

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

我想使用浏览按钮导入两个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, 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()


欢迎来到堆栈溢出!考虑将代码封装到代码块中,以提高您的问题的可读性以及快速回答的可能性。您正在传递一个按钮到 FieleLoist1这正是错误告诉您的。为什么你认为你应该给这个函数传递一个按钮?我想把tkinter和openpyxl结合起来。我认为可以使用浏览器按钮导入xlsx,然后使用openpyxl处理该excel。但是我不知道该怎么办。。