def excel_header(df: pd.DataFrame, sheet: xw.Sheet) -> xw.Range: try: current_app = sheet.book.app temp_excel_path = temp_path('.xlsx') df.head(1).to_excel(temp_excel_path) xl_header = current_app.books.open(temp_excel_path).sheets[0] xl_header_last_cell_address = xl_header.cells.api.SpecialCells( C.xlLastCell).Address xl_header_last_row = xw.Range(xl_header_last_cell_address).row xl_header.range('A1', xl_header_last_cell_address).api.Copy( sheet.range('A1').api) sheet.activate() sheet.api.Rows(xl_header_last_row).Delete() return sheet.cells(xl_header_last_row, 1) except Exception as e: raise e finally: xl_header.book.close() os.unlink(temp_excel_path)