0

Hello I want to make the changes in Excel and export updated excel which is imported using tkinder pandas python

CODE:

import tkinter as tk
from tkinter import filedialog, messagebox, ttk

import pandas as pd

root = tk.Tk()
root.title("Data Cleaning")
root.geometry("500x500") 
root.pack_propagate(False) 
root.resizable(0, 0) 

frame1 = tk.LabelFrame(root, text="Excel Data")
frame1.place(height=300, width=500)

file_frame = tk.LabelFrame(root, text="Open File")
file_frame.place(height=150, width=400, rely=0.70, relx=0)

button1 = tk.Button(file_frame, text="  Browse A File  ", command=lambda: File_dialog())
button1.place(rely=0.20, relx=0.20)

button2 = tk.Button(file_frame,  text="  Load File  ", command=lambda: Load_excel_data())
button2.place(rely=0.20, relx=0.50)

button3 = tk.Button(file_frame, text="Clean Scraped Data", command=lambda: Clean _data())
button3.place(rely=0.40, relx=0.20)


button5 = tk.Button(file_frame, text="Export", command=lambda: export())
button5.place(rely=0.65, relx=0.20)



label_file = ttk.Label(file_frame, text="No File Selected")
label_file.place(rely=0, relx=0)



tv1 = ttk.Treeview(frame1)
tv1.place(relheight=1, relwidth=1) 

treescrolly = tk.Scrollbar(frame1, orient="vertical", command=tv1.yview) 
treescrollx = tk.Scrollbar(frame1, orient="horizontal", command=tv1.xview) tv1.configure(xscrollcommand=treescrollx.set, yscrollcommand=treescrolly.set) 
treescrollx.pack(side="bottom", fill="x") 
treescrolly.pack(side="right", fill="y") 

def File_dialog():

    filename = filedialog.askopenfilename(initialdir="/",
                                          title="Select A File",
                                          filetype=(("xlsx files", "*.xlsx"),("All Files", "*.*")))
    label_file["text"] = filename
    return None

def Load_excel_data():
    file_path = label_file["text"]
    try:
        excel_filename = r"{}".format(file_path)
        if excel_filename[-4:] == ".csv":
            df = pd.read_csv(excel_filename)
        else:
            df = pd.read_excel(excel_filename)

    except ValueError:
        tk.messagebox.showerror("Information", "The file you have chosen is invalid")
        return None
    except FileNotFoundError:
        tk.messagebox.showerror("Information", f"No such file as {file_path}")
        return None
    file=pd.DataFrame(df)
    clear_data()
    tv1["column"] = list(df.columns)
    tv1["show"] = "headings"
    for column in tv1["columns"]:
        tv1.heading(column, text=column) 
    df_rows = df.to_numpy().tolist() 
    for row in df_rows:
        tv1.insert("", "end", values=row) 
        
    return None


def clear_data():
    tv1.delete(*tv1.get_children())
    return None

def Clean_data():
     df = df.rename(columns={'BirthDate':'Birthdate'})  
     return None

def Export():
     df.to_csv("Updated_excel.csv",index=False)


root.mainloop()

How do I export the excel and how to make changes? please explain what function should i use or how to store the excel in a data frame and then edit it accordingly ...................................................................................................................................................................................................................................................................................

  • 1
    you have to pass DataFrame between functions and return updated function. in your case functions always `return None` and you never pass any dataframe to `Clean_data()` function or `Export()` function. They should accept dataframe you use, something like `Clean_data(temp_DF)` – NoobVB Sep 06 '22 at 11:05
  • @NoobVB can you please help me pass dataframe if we use import function and save the imported excel in dataframe to make cahnges or export the updated file – vishakha abnave Sep 13 '22 at 05:36
  • 1
    hi again, please read about simple solution with global variables here: https://stackoverflow.com/questions/40460174/how-to-hand-over-a-dataframe-between-tkinter-buttons – NoobVB Sep 14 '22 at 15:30
  • 1
    but... I would Strongly suggest investing time into Classes and read the solution described here: https://stackoverflow.com/questions/45644243/pass-data-frame-through-tkinter-classes – NoobVB Sep 14 '22 at 15:32

0 Answers0