# Check if output file already exists if os.path.exists(output_file): book = openpyxl.load_workbook(output_file) if "Input_Data" in book.sheetnames: ws = book["Input_Data"] # Clear the entire contents of the sheet for row in ws: for cell in row: cell.value = None with pd.ExcelWriter(output_file, engine='openpyxl') as writer: writer.book = book writer.sheets = {ws.title: ws for ws in book.worksheets} final_df.to_excel(writer, sheet_name="Input_Data", index=False, startrow=0, startcol=0) else: # If the output file doesn't exist, create it final_df.to_excel(output_file, sheet_name="Input_Data", index=False, engine='openpyxl')