From e6c01557588055ef9bed4579d0100f4cc5a31744 Mon Sep 17 00:00:00 2001 From: "ewerton.almeida" Date: Wed, 13 Aug 2025 21:51:49 -0300 Subject: [PATCH] Quatro casas decimais ao gerar o excel para os campos ICMS (%), ICMS (%) (UF/Ref), Dif. ICMS (pp), PIS (%), COFINS (%) --- app/main.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) 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,