Reputation: 359
I created the following trial code to make a program that will load an excel sheet into a dataframe and then save that dataframe to another excel sheet (all of the user's choosing)
import os
import pandas
from pandas import ExcelWriter
import tkinter as tk
from tkinter import filedialog
class Load_Save_Program():
def __init__(self,master):
self.master = master
self.button1=tk.Button(self.master,text="Load",command=self.Load_file)
self.button1.grid(row=0,column=0)
self.button2=tk.Button(self.master,text="Save",command=self.Save_file)
self.button2.grid(row=0,column=1)
self.text=tk.Text(master)
self.text.grid(row=1,column=0,columnspan=2)
def Load_file(self):
self.df_import=pandas.read_excel(filedialog.askopenfilename(initialdir = os.getcwd()),
filetypes=("excel files","*.xlsx"))
self.text.insert(tk.END,self.df_import)
def Save_file(self):
self.writer = ExcelWriter(filedialog.asksaveasfilename(initialdir = os.getcwd()),
filetypes=("Excel files", "*.xlsx"))
self.df_import.to_excel(self.writer,'sheet1')
self.writer.save()
root=tk.Tk()
Load_Save_Program(root)
root.mainloop()
What I would like to do is to expand this so that when the program pops up the file directory window, it only shows files that are of the .xlsx filetype as to avoid an error from the user opening up an incompatible file type. So far I've yet to come up with any information that can explain how to set this up properly.
Upvotes: 6
Views: 16719
Reputation: 1
I stumbled upon this discussion when looking for a solution to a different problem. As the accepted answer cost me some time and did not help, I thought I'd clarify: The order of the kwargs in question for askopenfilename has no relevance.
In fact, I got it working with the exact order stated in the OP. The problem in the OP comes from misplaced parentheses, as stated in the comment by @j_4321 to the OP.
To sum up: OPs problem can be solved with the following:
Change
def Load_file(self):
self.df_import=pandas.read_excel(filedialog.askopenfilename(initialdir = os.getcwd()),
filetypes=("excel files","*.xlsx"))
To
def Load_file(self):
self.df_import=pandas.read_excel(filedialog.askopenfilename(initialdir = os.getcwd(),
filetypes=("excel files","*.xlsx")))
Changing the order of the kwargs when calling askopenfilename is not likely to solve problems.
Upvotes: 0
Reputation: 359
It turns out that the order is important. Additionally, the answer must include the [] around the filetype. The two lines in question must be
self.df_import=pandas.read_excel(filedialog.askopenfilename(filetypes=[("Excel files","*.xlsx")],initialdir = os.getcwd()))
self.writer = ExcelWriter(filedialog.asksaveasfilename(filetypes=[("Excel files", "*.xlsx")],initialdir = os.getcwd()))
Upvotes: 1
Reputation: 16169
The filedalogs have a filetypes option to do exactly what you want. The general syntax is filetypes=[(label1, ext1), (label2, ext2), ...]
.
In your case that will give:
filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx")])
Upvotes: 11