0

How do I edit spreadsheets using pandas, or any other library.

I have a CSV where I do the data reading and some filters, which I intend to save in an XLSX worksheet ready.

But when I try to send the dataframe to this XLSX worksheet, the file is overwritten by removing all existing edits and sheets in the worksheet.

I'm trying to do so.

excel_name = 'data/nessus/My Scans/Janeiro_2019/teste.xlsx'
writer = pd.ExcelWriter(excel_name,  engine='xlsxwriter')
df5.to_excel(writer, sheet_name='FullExport', index=False)
workbook=writer.book
worksheet = writer.sheets['FullExport']    
writer.save()

I think I'm doing something wrong, but I can not solve it.

PS:

This dataframe should be sent to the sheet called "FullExport" on line 2

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • This will help: https://stackoverflow.com/questions/20219254/how-to-write-to-an-existing-excel-file-without-overwriting-data-using-pandas – DMSTA Jan 22 '19 at 14:21
  • Run with out the 4th and 5th lines in your code. After writing to excel using `to_excel`, `writer.save()` would do – Sumanth Jan 22 '19 at 14:21

1 Answers1

1

In pandas version 0.24 they will be an option for mode='a'; however; right now you will have to:

writer = pd.ExcelWriter(excel_name, engine='openpyxl')
writer.book = load_workbook(excel_name)
df5.to_excel(writer, sheet_name='FullExport', index=False)
writer.save()
write.close() # i think close() already runs the save function above
It_is_Chris
  • 13,504
  • 2
  • 23
  • 41