Reputation: 673
Scenario:
Below is the snippet of my code:
for sheet in all_sheets: # all_sheets is the list of sheets from the input file
print(sheet) # Here i transform the data but removed the
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
Problem Statement:
Can anyone please guide me where i am going wrong?
Other approches:
for sheet in all_sheets:
writer = pd.ExcelWriter('Output.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, encoding='utf-8', index=True)
writer.save()
for sheet in all_sheets[8:10]:
print(sheet)
writer = pd.ExcelWriter('newfile.xlsx', engine='xlsxwriter')
df_final_4.to_excel(writer, sheet_name=sheet, index=True)
writer.save()
wb = openpyxl.Workbook()
wb.save('Output.xlsx')
book = load_workbook('Output.xlsx')
writer = pd.ExcelWriter('Output.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df_final_4.to_excel(writer, sheet, index=True)
writer.save()
writer.close()
Upvotes: 0
Views: 4941
Reputation: 1
I managed to solve this using dictionaries. create a dictionary with basic keys and the below code hellped:
df_dict = {}
for i in range(len(df)):
df_dict[i] = df[i]
writer = pd.ExcelWriter(path,engine = 'xlsxwriter')
for name,value in df_dict.items():
value.to_excel(writer,sheet_name = 'x'+str(name))
print("sheet {} written".format(name))
writer.save()
this created new sheets with the multiple dataframes.
Upvotes: 0
Reputation: 673
I was able to solve the issue by changing the sequence of the code:
writer = ExcelWriter('Output.xlsx')
for sheet in all_sheets:
<do calculations>
<do manipulations>
<do slicing>
df.to_excel(writer, sheet_name=sheet)
writer.save()
This is basically because we want to write multiple sheets in the same file so the initialization or definition of the writer should be only done once.
However, in case, only one sheet is required to be written then the writer can be defined anywhere inside the loop.
Upvotes: 2