How to save data in same Excel sheet using ExcelWriter in python?

Viewed 25

I am trying to save data for e.g: enter image description here in Sheet1 but the data is getting saved into another sheet.

df.to_excel('new_workbook.xlsx',sheet_name='Sheet1',index=False) I am generating a new excel file with pd.ExcelWriter, but it throws an error path to file not found if I don't generate the excel file. As I don't want my file to get overridden I am using mode='a'. If I don't use mode='a' the data will get saved to same sheet1. Below is my code, please review it and let me know the solution

import pandas as pd

df = pd.DataFrame()
df.to_excel('new_workbook.xlsx',sheet_name='Sheet1',index=False)   #Genrating excel file so it can work with ExcelWriter

empty_list = []

for i in range(1, 11):
    temp = {
        "counter":i,
        "bar":"check"
    }
    empty_list.append(temp)

print(empty_list)
with pd.ExcelWriter('new_workbook.xlsx',mode='a',engine="openpyxl",if_sheet_exists='overlay') as writer:
    pd.DataFrame(empty_list).to_excel(writer,sheet_name='Sheet1',index=False)

0 Answers
Related