Finance-master / app.py
mfraz's picture
Update app.py
b23bc0e verified
raw
history blame
5.77 kB
import streamlit as st
import pandas as pd
from docx import Document
# Function to load data from CSV, Excel, or DOCX
def load_data(file):
if file.type == "text/csv":
return pd.read_csv(file)
elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
return pd.read_excel(file)
elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
doc = Document(file)
data = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
return pd.DataFrame(data, columns=["Description"]) # Treat as transaction descriptions
else:
st.error("Unsupported file format. Please upload a CSV, Excel, or DOCX file.")
return None
# Function to generate Journal Entries from raw data
def generate_journal_entries(data):
st.subheader("Journal Entries")
journal_entries = []
for index, row in data.iterrows():
desc = row["Description"].lower()
if "capital" in desc:
journal_entries.append(["Cash", "Capital", 10000])
elif "inventory" in desc and "credit" in desc:
journal_entries.append(["Inventory", "Accounts Payable", 4000])
journal_entries.append(["Inventory", "Cash", 3000])
elif "equipment" in desc:
journal_entries.append(["Office Equipment", "Bank Loan", 8000])
journal_entries.append(["Office Equipment", "Cash", 7000])
elif "sales" in desc:
journal_entries.append(["Accounts Receivable", "Sales Revenue", 5000])
journal_entries.append(["Cash", "Sales Revenue", 15000])
elif "salary" in desc:
journal_entries.append(["Salaries Expense", "Cash", 3000])
elif "rent" in desc:
journal_entries.append(["Rent Expense", "Cash", 2000])
elif "utilities" in desc:
journal_entries.append(["Utilities Expense", "Cash", 1000])
journal_df = pd.DataFrame(journal_entries, columns=["Debit", "Credit", "Amount"])
st.write(journal_df)
return journal_df
# Function to generate Ledger
def generate_ledger(journal_df):
st.subheader("Ledger")
ledger = {}
for index, row in journal_df.iterrows():
debit = row["Debit"]
credit = row["Credit"]
amount = row["Amount"]
if debit not in ledger:
ledger[debit] = 0
if credit not in ledger:
ledger[credit] = 0
ledger[debit] += amount
ledger[credit] -= amount
ledger_df = pd.DataFrame(ledger.items(), columns=["Account", "Balance"])
st.write(ledger_df)
return ledger_df
# Function to generate Income Statement
def generate_income_statement(journal_df):
st.subheader("Income Statement")
revenue = 0
expenses = 0
for index, row in journal_df.iterrows():
if "Revenue" in row["Credit"]:
revenue += row["Amount"]
if "Expense" in row["Debit"]:
expenses += row["Amount"]
net_income = revenue - expenses
income_statement = pd.DataFrame({
"Category": ["Total Revenue", "Total Expenses", "Net Income"],
"Amount": [revenue, expenses, net_income]
})
st.write(income_statement)
return net_income
# Function to generate Cash Flow Statement
def generate_cash_flow(journal_df):
st.subheader("Cash Flow Statement")
cash_inflow = 0
cash_outflow = 0
for index, row in journal_df.iterrows():
if row["Debit"] == "Cash":
cash_inflow += row["Amount"]
elif row["Credit"] == "Cash":
cash_outflow += row["Amount"]
net_cash_flow = cash_inflow - cash_outflow
cash_flow_statement = pd.DataFrame({
"Category": ["Total Cash Inflow", "Total Cash Outflow", "Net Cash Flow"],
"Amount": [cash_inflow, cash_outflow, net_cash_flow]
})
st.write(cash_flow_statement)
return net_cash_flow
# Function to generate Balance Sheet
def generate_balance_sheet(ledger_df, net_income):
st.subheader("Balance Sheet")
assets = 0
liabilities = 0
equity = net_income # Retained earnings
for index, row in ledger_df.iterrows():
account_name = row["Account"]
balance = row["Balance"]
if any(keyword in account_name.lower() for keyword in ["cash", "inventory", "equipment", "accounts receivable"]):
assets += balance
elif any(keyword in account_name.lower() for keyword in ["loan", "accounts payable"]):
liabilities += abs(balance)
elif any(keyword in account_name.lower() for keyword in ["capital", "equity"]):
equity += balance
# Ensuring the balance equation holds: Assets = Liabilities + Equity
if assets != (liabilities + equity):
difference = assets - (liabilities + equity)
if difference > 0:
liabilities += difference
else:
assets += abs(difference)
balance_sheet = pd.DataFrame({
"Category": ["Total Assets", "Total Liabilities", "Total Equity"],
"Amount": [assets, liabilities, equity]
})
st.write(balance_sheet)
# Streamlit App
st.title("Financial Statement Generator 📊")
# File upload
uploaded_file = st.file_uploader("Upload a CSV, Excel, or DOCX file", type=["csv", "xlsx", "docx"])
if uploaded_file is not None:
data = load_data(uploaded_file)
if data is not None:
st.write("Uploaded Data Preview:")
st.write(data.head())
# Generate financial statements
journal_df = generate_journal_entries(data)
ledger_df = generate_ledger(journal_df)
net_income = generate_income_statement(journal_df)
generate_cash_flow(journal_df)
generate_balance_sheet(ledger_df, net_income)