Skip to content

Instantly share code, notes, and snippets.

@leogregianin
Last active March 19, 2025 13:40
Show Gist options
  • Select an option

  • Save leogregianin/7bd1e3976da99a03addcfed117cba21a to your computer and use it in GitHub Desktop.

Select an option

Save leogregianin/7bd1e3976da99a03addcfed117cba21a to your computer and use it in GitHub Desktop.
planilha de custos com pandas
import pandas as pd
# Caminho do arquivo
file_path = "/mnt/data/controle.xlsx"
# Criando um arquivo Excel com múltiplas abas usando xlsxwriter
with pd.ExcelWriter(file_path, engine="xlsxwriter") as writer:
workbook = writer.book
# Criando dados para a aba "Lançamentos"
df_lancamentos = pd.DataFrame({
"Data": [""],
"Placa do Veículo": [""],
"Quilometragem Rodada": [0],
"Combustível (R$)": [0],
"Manutenção (R$)": [0],
"Pneus (R$)": [0],
"Seguro (R$)": [0],
"Depreciação (R$)": [0],
"Salários Motoristas (R$)": [0],
"Pedágios e Taxas (R$)": [0],
"Outros Custos (R$)": [0],
"Custo Total (R$)": [0], # Será sobrescrito com fórmula
"Faturamento (R$)": [0],
"Lucro/Prejuízo (R$)": [0] # Será sobrescrito com fórmula
})
df_lancamentos.to_excel(writer, sheet_name="Lançamentos", index=False)
worksheet_lancamentos = writer.sheets["Lançamentos"]
# Adicionando fórmulas nas colunas "Custo Total" e "Lucro/Prejuízo"
worksheet_lancamentos.write_formula("L2", "=SUM(D2:K2)")
worksheet_lancamentos.write_formula("N2", "=M2-L2")
# Criando dados para a aba "Resumo"
df_resumo = pd.DataFrame({
"Placa do Veículo": [""],
"Total KM Rodado": [0], # Será sobrescrito com fórmula
"Custo Total (R$)": [0], # Será sobrescrito com fórmula
"Faturamento Total (R$)": [0], # Será sobrescrito com fórmula
"Lucro/Prejuízo Total (R$)": [0] # Será sobrescrito com fórmula
})
df_resumo.to_excel(writer, sheet_name="Resumo", index=False)
worksheet_resumo = writer.sheets["Resumo"]
# Adicionando fórmulas na aba "Resumo"
worksheet_resumo.write_formula("B2", "=SUMIF('Lançamentos'!B:B,A2,'Lançamentos'!C:C)")
worksheet_resumo.write_formula("C2", "=SUMIF('Lançamentos'!B:B,A2,'Lançamentos'!L:L)")
worksheet_resumo.write_formula("D2", "=SUMIF('Lançamentos'!B:B,A2,'Lançamentos'!M:M)")
worksheet_resumo.write_formula("E2", "=D2-C2")
file_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment