Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python tkinter中上载excel文件并作为数据帧打印_Python_File_User Interface_Tkinter_Upload - Fatal编程技术网

在Python tkinter中上载excel文件并作为数据帧打印

在Python tkinter中上载excel文件并作为数据帧打印,python,file,user-interface,tkinter,upload,Python,File,User Interface,Tkinter,Upload,我想创建一个包含上传文件功能的接口。我的意思是,当我点击“上传文件”按钮时,会出现一个弹出窗口,我会选择excel文件。在那之后,我应该把它作为一个数据帧。我尝试了一些解决办法,但没有找到实际效果。顺便说一句,我的解决方案不是很有效。正如可能看到的,我尝试获取路径,然后尝试获取数据帧 import tkinter as tk from tkinter import * from datetime import date import time from datetime import timed

我想创建一个包含上传文件功能的接口。我的意思是,当我点击“上传文件”按钮时,会出现一个弹出窗口,我会选择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'
Tkinter回调中出现异常 回溯(最近一次呼叫最后一次): 文件“C:\Program Files\Anaconda3\lib\tkinter\\uuuuu init\uuuuu.py”,第1550行,在调用中__ 返回self.func(*args) 文件“”,第118行,正在上载 file2=filem1.name AttributeError:“str”对象没有属性“name”
askopenfilename返回包含文件路径的字符串。字符串没有名为“name”的方法或属性。你可以写:

file2 = filem1

另一方面,您可以使用os.path库格式化“df_cities=read_excel(filem4)”之前的文件名。这将允许您的代码独立于Windows和Linux之间的正斜杠/反斜杠差异。非常感谢,我完全理解了这一点,并且很容易解决了我的问题:))