在Python tkinter中上载excel文件并作为datafram打印

2024-04-20 08:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个包含上传文件功能的接口。我的意思是,当我点击“上传文件”按钮,一个弹出窗口会出现,我会选择关于excel文件。在那之后,我应该把它作为一个数据帧。我尝试了一些解决办法,但没有找到实际的结果。顺便说一句,我的解决方案不是很有效。正如我所看到的,我尝试获取路径,然后尝试获取数据帧。你知道吗

import tkinter as tk
from tkinter import *
from datetime import date
import time
from datetime import timedelta
import os, os.path
import glob
import os
import pandas as pd
from pandas import DataFrame, read_excel, merge, read_csv
from tkinter.filedialog import askopenfilename
import tkinter.filedialog as fdialog
import pandas as pd
from pandas import DataFrame, read_excel, merge, read_csv
.
.
.

class Page2(Page):
    def __init__(self, *args, **kwargs):
        Page.__init__(self, *args, **kwargs)
        label = tk.Label(self, text="", bg="white")
        button_2=tk.Button(self,text="Om Creation", width=10,height=3 ,fg="white",bg="blue",font=("Arial",10), command=self.OMcreat).place(x=750,y=400)
        label.pack(side="top", fill="both", expand=True)

        button_4=tk.Button(self,text="upload file", width=12,height=3,fg="white",bg="blue",font=("Arial",10), command=self.upload).place(x=350,y=450)




    def upload(self):
        print("uploaded")        

        #root.directory = tkFileDialog.askdirectory()
        #print (root.directory)



        filem1 = askopenfilename(filetypes=(("Template files", "*.tplate"),
                                           ("HTML files", "*.html;*.htm"),
                                           ("All files", "*.*") ))


        print(filem1)

        file2=filem1.name


        filem3=str(file2)
        print(filem3)
        filem4=filem3.replace("/", "\\")
        print(filem4)
        df_cities=read_excel(filem4)
        print(df_cities.head())
        reportname=df_cities.at[1,'Report_Name']
        print(reportname)
        df_cities.head()
        df=open(file2)
        df_excelim=read_excel(filem1)

错误是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Anaconda3\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "<ipython-input-14-88767c64d414>", line 118, in upload
    file2=filem1.name
AttributeError: 'str' object has no attribute 'name'

Tags: fromimportselfpandasdfreadtkinteras