mfraz commited on
Commit
2aa66f9
·
verified ·
1 Parent(s): 8995ab7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -93
app.py CHANGED
@@ -1,109 +1,94 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  from docx import Document
4
- from io import BytesIO
5
 
6
- def process_financial_data(file):
7
- try:
8
- if file.name.endswith('.csv'):
9
- df = pd.read_csv(file)
10
- elif file.name.endswith(('.xls', '.xlsx')):
11
- df = pd.read_excel(file)
12
- elif file.name.endswith('.docx'):
13
- doc = Document(file)
14
- data = [para.text for para in doc.paragraphs if para.text.strip()]
15
- df = pd.DataFrame({'Content': data})
16
- else:
17
- st.error("Unsupported file format. Please upload a CSV, Excel, or Word document.")
18
- return None, None, None, None, None, None
19
-
20
- # Creating Financial Reports
21
- ledger = df[df['Type'] == 'Ledger'] if 'Type' in df.columns else None
22
- journal = df[df['Type'] == 'Journal'] if 'Type' in df.columns else None
23
- trial_balance = df[df['Type'] == 'Trial Balance'] if 'Type' in df.columns else None
24
- income_statement = df[df['Type'] == 'Income Statement'] if 'Type' in df.columns else None
25
- balance_sheet = df[df['Type'] == 'Balance Sheet'] if 'Type' in df.columns else None
26
- cash_flow = df[df['Type'] == 'Cash Flow Statement'] if 'Type' in df.columns else None
27
-
28
- return ledger, journal, trial_balance, income_statement, balance_sheet, cash_flow
29
- except Exception as e:
30
- st.error(f"Error processing file: {e}")
31
- return None, None, None, None, None, None
32
 
33
- def generate_word_report(ledger, journal, trial_balance, income_stmt, balance_sht, cash_flw):
34
- doc = Document()
35
- doc.add_heading("Financial Report", level=1)
36
- doc.add_paragraph("\nGenerated Financial Statements\n")
37
 
38
- def add_table(df, title):
39
- if df is not None and not df.empty:
40
- doc.add_heading(title, level=2)
41
- table = doc.add_table(rows=1, cols=len(df.columns))
42
- table.style = 'Table Grid'
43
-
44
- hdr_cells = table.rows[0].cells
45
- for i, col_name in enumerate(df.columns):
46
- hdr_cells[i].text = col_name
47
 
48
- for _, row in df.iterrows():
49
- row_cells = table.add_row().cells
50
- for i, val in enumerate(row):
51
- row_cells[i].text = str(val)
 
 
 
 
 
 
 
 
52
 
53
- doc.add_paragraph("\n")
54
- else:
55
- doc.add_heading(title, level=2)
56
- doc.add_paragraph("No data available.\n")
 
 
 
 
 
 
 
 
57
 
58
- # Add each financial statement to the document
59
- add_table(ledger, "Ledger")
60
- add_table(journal, "Journal Entries")
61
- add_table(trial_balance, "Trial Balance")
62
- add_table(income_stmt, "Income Statement")
63
- add_table(balance_sht, "Balance Sheet")
64
- add_table(cash_flw, "Cash Flow Statement")
 
 
 
 
 
65
 
66
- buffer = BytesIO()
67
- doc.save(buffer)
68
- buffer.seek(0)
69
- return buffer
70
 
71
- st.title("Comprehensive Financial Accounting App")
72
- st.write("Upload a financial data file (CSV, Excel, Word) to generate Ledger, Journal, and Financial Statements.")
73
-
74
- uploaded_file = st.file_uploader("Upload a file", type=["csv", "xls", "xlsx", "docx"])
75
 
76
  if uploaded_file is not None:
77
- ledger, journal, trial_balance, income_stmt, balance_sht, cash_flw = process_financial_data(uploaded_file)
78
-
79
- if ledger is not None:
80
- st.subheader("Ledger")
81
- st.dataframe(ledger)
82
-
83
- if journal is not None:
84
- st.subheader("Journal Entries")
85
- st.dataframe(journal)
86
-
87
- if trial_balance is not None:
88
- st.subheader("Trial Balance")
89
- st.dataframe(trial_balance)
90
-
91
- if income_stmt is not None:
92
- st.subheader("Income Statement")
93
- st.dataframe(income_stmt)
94
-
95
- if balance_sht is not None:
96
- st.subheader("Balance Sheet")
97
- st.dataframe(balance_sht)
98
-
99
- if cash_flw is not None:
100
- st.subheader("Cash Flow Statement")
101
- st.dataframe(cash_flw)
102
-
103
- if st.button("Download Financial Report as Word Document"):
104
- doc_file = generate_word_report(ledger, journal, trial_balance, income_stmt, balance_sht, cash_flw)
105
- st.download_button(label="Download Report", data=doc_file, file_name="Financial_Report.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
106
 
107
- st.write("Developed for comprehensive financial analysis.")
 
 
 
 
 
108
 
109
 
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import numpy as np
4
  from docx import Document
 
5
 
6
+ # Function to load data from CSV, Excel, or DOCX
7
+ def load_data(file):
8
+ if file.type == "text/csv":
9
+ return pd.read_csv(file)
10
+ elif file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
11
+ return pd.read_excel(file)
12
+ elif file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
13
+ doc = Document(file)
14
+ data = []
15
+ for para in doc.paragraphs:
16
+ data.append(para.text)
17
+ return pd.DataFrame(data, columns=["Text"])
18
+ else:
19
+ st.error("Unsupported file format. Please upload a CSV, Excel, or DOCX file.")
20
+ return None
 
 
 
 
 
 
 
 
 
 
 
21
 
22
+ # Function to generate general entries
23
+ def generate_general_entries(data):
24
+ st.subheader("General Entries")
25
+ st.write(data.head()) # Display the first few rows of the data
26
 
27
+ # Function to generate ledger
28
+ def generate_ledger(data):
29
+ st.subheader("Ledger")
30
+ if "Account" in data.columns and "Amount" in data.columns:
31
+ ledger = data.groupby("Account")["Amount"].sum().reset_index()
32
+ st.write(ledger)
33
+ else:
34
+ st.error("The uploaded file must contain 'Account' and 'Amount' columns for ledger generation.")
 
35
 
36
+ # Function to generate income statement
37
+ def generate_income_statement(data):
38
+ st.subheader("Income Statement")
39
+ if "Revenue" in data.columns and "Expenses" in data.columns:
40
+ total_revenue = data["Revenue"].sum()
41
+ total_expenses = data["Expenses"].sum()
42
+ net_income = total_revenue - total_expenses
43
+ st.write(f"Total Revenue: {total_revenue}")
44
+ st.write(f"Total Expenses: {total_expenses}")
45
+ st.write(f"Net Income: {net_income}")
46
+ else:
47
+ st.error("The uploaded file must contain 'Revenue' and 'Expenses' columns for income statement generation.")
48
 
49
+ # Function to generate cash flow statement
50
+ def generate_cash_flow(data):
51
+ st.subheader("Cash Flow Statement")
52
+ if "Cash Inflow" in data.columns and "Cash Outflow" in data.columns:
53
+ total_inflow = data["Cash Inflow"].sum()
54
+ total_outflow = data["Cash Outflow"].sum()
55
+ net_cash_flow = total_inflow - total_outflow
56
+ st.write(f"Total Cash Inflow: {total_inflow}")
57
+ st.write(f"Total Cash Outflow: {total_outflow}")
58
+ st.write(f"Net Cash Flow: {net_cash_flow}")
59
+ else:
60
+ st.error("The uploaded file must contain 'Cash Inflow' and 'Cash Outflow' columns for cash flow statement generation.")
61
 
62
+ # Function to generate balance sheet
63
+ def generate_balance_sheet(data):
64
+ st.subheader("Balance Sheet")
65
+ if "Assets" in data.columns and "Liabilities" in data.columns and "Equity" in data.columns:
66
+ total_assets = data["Assets"].sum()
67
+ total_liabilities = data["Liabilities"].sum()
68
+ total_equity = data["Equity"].sum()
69
+ st.write(f"Total Assets: {total_assets}")
70
+ st.write(f"Total Liabilities: {total_liabilities}")
71
+ st.write(f"Total Equity: {total_equity}")
72
+ else:
73
+ st.error("The uploaded file must contain 'Assets', 'Liabilities', and 'Equity' columns for balance sheet generation.")
74
 
75
+ # Streamlit App
76
+ st.title("Financial Calculator 📊")
 
 
77
 
78
+ # File upload
79
+ uploaded_file = st.file_uploader("Upload a CSV, Excel, or DOCX file", type=["csv", "xlsx", "docx"])
 
 
80
 
81
  if uploaded_file is not None:
82
+ data = load_data(uploaded_file)
83
+ if data is not None:
84
+ st.write("Uploaded Data Preview:")
85
+ st.write(data.head())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
+ # Generate financial statements
88
+ generate_general_entries(data)
89
+ generate_ledger(data)
90
+ generate_income_statement(data)
91
+ generate_cash_flow(data)
92
+ generate_balance_sheet(data)
93
 
94