1

After saving, all sheets in the Excel file are deleted except for the one to which I wrote new data, how to fix it?

def teach_add_3():
with pd.ExcelWriter('bd1.xlsx', engine='openpyxl', mode='a') as writer:
    df.to_excel(writer, sheet_name='teachers')
    teacher = input("Enter teacher: ")
    subject = input("Enter subject: ")
    df.loc[index, 'Преподаватели'] = teacher
    df.loc[index, 'Предметы'] = subject
    df.to_excel(writer, sheet_name='teachers')
Umar.H
  • 22,559
  • 7
  • 39
  • 74
HeyBigMars
  • 35
  • 5
  • 1
    Does this answer your question? [Append existing excel sheet with new dataframe using python pandas](https://stackoverflow.com/questions/38074678/append-existing-excel-sheet-with-new-dataframe-using-python-pandas) – Aurèle Dec 17 '20 at 08:51
  • I think [this](https://stackoverflow.com/a/54188876/9375102) answer is more appropriate if your using a newer pandas version. – Umar.H Dec 17 '20 at 08:57

1 Answers1

-1

In this case, we would have open our own ExcelWriter with the "append" mode like this:

with pd.ExcelWriter('bd1.xlsx', engine='openpyxl', mode='a') as writer:  
    df.to_excel(writer, sheet_name='teachers')

Make sure to install the openpyxl module.

pip install openpyxl
Abe Malla
  • 67
  • 1
  • 4