Quatro casas decimais ao gerar o excel para os campos ICMS (%), ICMS (%) (UF/Ref), Dif. ICMS (pp), PIS (%), COFINS (%)
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
30
app/main.py
30
app/main.py
@@ -487,12 +487,30 @@ async def export_excel(
|
|||||||
})
|
})
|
||||||
filename = "relatorio_geral.xlsx"
|
filename = "relatorio_geral.xlsx"
|
||||||
|
|
||||||
# 3) Excel em memória
|
# 3) Excel em memória
|
||||||
output = BytesIO()
|
output = BytesIO()
|
||||||
df = pd.DataFrame(dados)
|
df = pd.DataFrame(dados)
|
||||||
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
|
||||||
df.to_excel(writer, index=False, sheet_name="Relatório")
|
# garante que colunas percentuais estejam numéricas (se existirem)
|
||||||
output.seek(0)
|
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(
|
return StreamingResponse(
|
||||||
output,
|
output,
|
||||||
|
|||||||
Reference in New Issue
Block a user