Pandas dataframe headers into a openpyxl table
Essentially I havent been able to parse a pandas dataframe into an excel table properly, i have the output working but the headers remain being passed as if the were an extra row
ws.append([])
ws.append(["2. Cálculo del valor actual:"])
ws.append([])
pv_df = pv_amortization_df[pv_amortization_df["contract_name"] == contract]
start_table_row = ws.max_row + 1
for r in dataframe_to_rows(pv_df, index=False, header=True):
ws.append(r)
end_table_row = ws.max_row
end_col_letter = chr(65 + pv_df.shape[1] - 1)
table_ref = f"A{start_table_row}:{end_col_letter}{end_table_row}"
table = Table(displayName=f"PVAmortization_{contract}", ref=table_ref)
table.tableStyleInfo = TableStyleInfo(name="TableStyleMedium9", showRowStripes=True)
ws.add_table(table)
# Auto-fit PV Amortization columns
for i, col_cells in enumerate(ws.iter_cols(min_col=1, max_col=pv_df.shape[1])):
max_length = max([len(str(cell.value)) for cell in col_cells if cell.value is not None] + [len(pv_df.columns[i])])
ws.column_dimensions[col_cells[0].column_letter].width = max_length + 2