The function gets called several times. I have kept a count so that if its called for the first time, A workbook is created. Then I write to that workbook using pd.ExcelWrite()
. Next time else:
gets executed and the same workbook is opened. Its first sheet is selected, its last row is found. And DataFrame is written on that row.
This is my code:
def WriteFile (df):
if count1 == 1:
workbook = xlsxwriter.Workbook('pandas_simple.xlsx')
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False )
workbook.close()
else:
book = open_workbook('pandas_simple.xlsx')
sheet_names = book.sheet_names()
sheet = book.sheet_by_name(sheet_names[0])
row = sheet.nrows
writer = pd.ExcelWriter('pandas_simple.xlsx', engine='xlsxwriter')
df.to_excel(writer, index=False, header=False, startrow = row )
I get this exception:
Exception Exception: Exception('Exception caught in workbook destructor. Explicit close()
may be required for workbook.',) in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook
object at 0x000000000A143860>> ignored Exception
And my pandas_simple.xlsx is also empty after execution of code. What am I doing wrong?