diff --git a/app/main.py b/app/main.py index f2390ce..d0daa2e 100644 --- a/app/main.py +++ b/app/main.py @@ -487,12 +487,30 @@ async def export_excel( }) filename = "relatorio_geral.xlsx" - # 3) Excel em memória - output = BytesIO() - df = pd.DataFrame(dados) - with pd.ExcelWriter(output, engine="xlsxwriter") as writer: - df.to_excel(writer, index=False, sheet_name="Relatório") - output.seek(0) + # 3) Excel em memória + output = BytesIO() + df = pd.DataFrame(dados) + + # garante que colunas percentuais estejam numéricas (se existirem) + for col in ["ICMS (%)", "ICMS (%) (UF/Ref)", "Dif. ICMS (pp)", "PIS (%)", "COFINS (%)"]: + if col in df.columns: + df[col] = pd.to_numeric(df[col], errors="coerce") + + with pd.ExcelWriter(output, engine="xlsxwriter") as writer: + df.to_excel(writer, index=False, sheet_name="Relatório") + + # aplica formatação 4 casas decimais + wb = writer.book + ws = writer.sheets["Relatório"] + fmt_4dec = wb.add_format({"num_format": "0.0000"}) + + for col in ["ICMS (%)", "ICMS (%) (UF/Ref)", "Dif. ICMS (pp)", "PIS (%)", "COFINS (%)"]: + if col in df.columns: + i = df.columns.get_loc(col) + # largura automática básica + formato; ajuste a largura se quiser (ex.: 12) + ws.set_column(i, i, None, fmt_4dec) + + output.seek(0) return StreamingResponse( output,