Friday, 3 May 2019

Check workbook for sheet and add if missing

I am trying to simply check if a sheet exists in an .xlsx file and if not I want to add it.

book = load_workbook('test.xlsx')
writer = pd.ExcelWriter('test.xlsx', engine = 'openpyxl')
writer.book = book

if 'testSheet' in book.sheetnames:
    pass
else:
    book.add_sheet(book['testSheet'])

Any ideas as to why this doesn't work?



from Check workbook for sheet and add if missing

No comments:

Post a Comment