使用tkinter和openpyxl导入excel表

时间:2017-05-12 11:23:45

标签: 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, 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:参数应该是字符串,字节或整数,而不是Button

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

问题是您正在传递一个按钮,文件名应该在该按钮上,请尝试如下操作:

首先导入所有模块

private _pageCount = new BehaviorSubject<number>(0);
count$ = this._pageCount.asObservable();

incrementPageCount(){
    const pageCount = this.af.object('/pageCount/').$ref
      .ref.transaction(count => {
        return count + 1;
      }).then((data) => {return data.snapshot.node_.value_;});

    return pageCount;
}

创建窗口:

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