在 Python tkinter 中上传 excel 文件并打印为数据框
Uploading excel file in Python tkinter and print as dataframe
我想创建一个包含上传文件功能的界面。我的意思是,当我单击 "upload the file" 按钮时,会出现一个弹出窗口,我将选择关于 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'
askopenfilename returns 包含文件路径的字符串。该字符串没有名为 "name" 的方法或属性。你可以这样写:
file2 = filem1
我想创建一个包含上传文件功能的界面。我的意思是,当我单击 "upload the file" 按钮时,会出现一个弹出窗口,我将选择关于 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'
askopenfilename returns 包含文件路径的字符串。该字符串没有名为 "name" 的方法或属性。你可以这样写:
file2 = filem1