Reputation: 33
I want to import two xlsx using a browse buttons :
This is the code i used:
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
But when I build the script , I receive this error: TypeError: argument should be string, bytes or integer, not Button
Is anyone which can help me ?
Upvotes: 1
Views: 5182
Reputation: 1124
The problem is that you are passing a button where the file_name should be, try like this:
Import all modules first
from tkinter import *
from tkinter.filedialog import askopenfile
from openpyxl import load_workbook
Create your window :
root = Tk()
root.geometry('200x100')
Then create your function :
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
then everything else :
btn = Button(root, text ='Open', command = open_file)
btn.pack(side='top')
mainloop()
Upvotes: 1