Issue
i'm working with a gui in Python, which i created via tkinter. Now i want to save some data(that i stored in a two dimensional List) in an excel file. I'm trying to do that in one function.
Here is the Code that i use
def Convert(self):
list_data_conv = self.list_data
df=pd.DataFrame(list_data_conv)
excel_file = '{0}.xlsx'.format(self.Entry_ExcelName.get())
if (self.excel_file_old==excel_file):
writer = pd.ExcelWriter(excel_file, engine='openpyxl')
else:
writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
self.excel_file_old=excel_file
sheet_name = '{0}'.format(self.Entry_SheetName.get())
df.to_excel(writer, sheet_name=sheet_name)
writer.save()
Using this code, everything works fine except that it still overwrites the Existing Sheet in the Excel File.
Is there a simple fix?
Solution
The ExcelWriter's default mode is "write", in order to append to it, set mode to append ('a').
Just replace it with :
writer = pd.ExcelWriter(excel_file, engine='openpyxl', mode='a')
If the code line above didn't work, try:
writer = pd.ExcelWriter(r'excel_file', engine='openpyxl', mode='a')
Sould work now.
Answered By - Nova
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.