简体   繁体   English

使用 tkinter 和 openpyxl 导入 Excel 表格

[英]Importing excel sheet using tkinter and openpyxl

I want to import two xlsx using a browse buttons :我想使用浏览按钮导入两个 xlsx:

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但是当我构建脚本时,我收到这个错误:TypeError: argument should be string, bytes or integer, not Button

Is anyone which can help me ?有人可以帮助我吗?

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM