簡體   English   中英

使用 tkinter 和 openpyxl 導入 Excel 表格

[英]Importing excel sheet using tkinter and openpyxl

我想使用瀏覽按鈕導入兩個 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: argument should be string, bytes or integer, not Button

有人可以幫助我嗎?

問題是你傳遞了一個按鈕,文件名應該是,試試這樣:

先導入所有模塊

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM