-1

I have two dataframes, which I would like to export to one excel file as different sheets. I am writing this for people who aren't familiar with Python and would like to give them the option to decide where to save the file. So far, I have the below code but I do not know how to edit it so it allows me to save multiple dataframes to different sheets. This only works for one DF at the moment. Any help appreciated!

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()

def exportExcel ():
    global df
    
    export_file_path = filedialog.asksaveasfilename(defaultextension='.xlsx')
    df.to_excel (export_file_path, index = False, header=True)

saveAsButtonExcel = tk.Button(text='Export Excel', command=exportExcel, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=saveAsButtonExcel)

root.mainloop()

I have df1 and df2 I would like to use embed into one excel file...thanks!

Jimmy K
  • 133
  • 11

1 Answers1

0
with pd.ExcelWriter('route to the file nd the name => sample.xlsx') as writer1:
    (name_of_dataframe1).to_excel(writer1,sheet_name="Orders")
    (name_of_dataframe2).to_excel(writer1,sheet_name="Suppliers")
    (name_of_dataframe3).to_excel(writer1,sheet_name="Products")
    (name_of_dataframe4).to_excel(writer1,sheet_name="Clients")

I have exported four dataframes from pandas to a single sheet named sample.xlsx. in four different sheets => orders,Suppliers,Products and clients