Borjamg commited on
Commit
eceeac2
·
verified ·
1 Parent(s): 5107379

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -24
app.py CHANGED
@@ -3,11 +3,12 @@ import pandas as pd
3
  import io
4
  import tempfile
5
  import re
 
6
  from openpyxl import load_workbook
7
  from openpyxl.styles import Font, PatternFill
8
 
9
  # Core processing logic
10
- def process_excel(data_bytes):
11
  buffer = io.BytesIO(data_bytes)
12
  xls = pd.ExcelFile(buffer)
13
  df = xls.parse(xls.sheet_names[0], header=None, dtype=str)
@@ -133,16 +134,18 @@ def process_excel(data_bytes):
133
  # Extract shareholders
134
  shareholders = add_company_name_row(df.iloc[b["shareholders"]:b["shareholders_history"]])
135
 
136
- # Write to in-memory Excel
137
- out = io.BytesIO()
138
- with pd.ExcelWriter(out, engine='openpyxl') as writer:
 
 
 
139
  balance_sheet.to_excel(writer, sheet_name="balance_sheet", index=False, header=False)
140
  cash_flow.to_excel(writer, sheet_name="cash_flow", index=False, header=False)
141
  shareholders.to_excel(writer, sheet_name="shareholders", index=False, header=False)
142
- out.seek(0)
143
 
144
- # Apply formatting
145
- wb = load_workbook(out)
146
  fill = PatternFill(start_color='C5D9F1', end_color='C5D9F1', fill_type='solid')
147
  bold = Font(bold=True)
148
 
@@ -158,14 +161,10 @@ def process_excel(data_bytes):
158
  if 'cash_flow' in wb.sheetnames:
159
  ws = wb['cash_flow']
160
  highlight_labels = {
161
- "cash flow from operations:",
162
- "'= net cash from operations",
163
- "cash flow from investing:",
164
- "'= net cash from investing",
165
- "cash flow from financing:",
166
- "'= net cash from financing",
167
- "net increase/(decrease) in cash",
168
- "'= cash at end of period"
169
  }
170
  for row in ws.iter_rows():
171
  first_cell = row[0]
@@ -196,23 +195,18 @@ def process_excel(data_bytes):
196
  cell.fill = fill
197
  cell.font = bold
198
 
199
- final = io.BytesIO()
200
- wb.save(final)
201
- final.seek(0)
202
- return final.getvalue()
203
 
204
  # Gradio wrapper
205
  def extract_and_download(uploaded_file):
 
206
  if hasattr(uploaded_file, "read"):
207
  data = uploaded_file.read()
208
  else:
209
  with open(uploaded_file, "rb") as f:
210
  data = f.read()
211
- out_bytes = process_excel(data)
212
- tmp_path = tempfile.mktemp(suffix=".xlsx")
213
- with open(tmp_path, "wb") as tmp:
214
- tmp.write(out_bytes)
215
- return tmp_path
216
 
217
  iface = gr.Interface(
218
  fn=extract_and_download,
 
3
  import io
4
  import tempfile
5
  import re
6
+ import os
7
  from openpyxl import load_workbook
8
  from openpyxl.styles import Font, PatternFill
9
 
10
  # Core processing logic
11
+ def process_excel(data_bytes, original_filename):
12
  buffer = io.BytesIO(data_bytes)
13
  xls = pd.ExcelFile(buffer)
14
  df = xls.parse(xls.sheet_names[0], header=None, dtype=str)
 
134
  # Extract shareholders
135
  shareholders = add_company_name_row(df.iloc[b["shareholders"]:b["shareholders_history"]])
136
 
137
+ # Save Excel file with same base name + '_formatted.xlsx'
138
+ base = os.path.splitext(os.path.basename(original_filename))[0]
139
+ filename = base + "_formatted.xlsx"
140
+ tmp_path = os.path.join(tempfile.gettempdir(), filename)
141
+
142
+ with pd.ExcelWriter(tmp_path, engine='openpyxl') as writer:
143
  balance_sheet.to_excel(writer, sheet_name="balance_sheet", index=False, header=False)
144
  cash_flow.to_excel(writer, sheet_name="cash_flow", index=False, header=False)
145
  shareholders.to_excel(writer, sheet_name="shareholders", index=False, header=False)
 
146
 
147
+ # Format with openpyxl
148
+ wb = load_workbook(tmp_path)
149
  fill = PatternFill(start_color='C5D9F1', end_color='C5D9F1', fill_type='solid')
150
  bold = Font(bold=True)
151
 
 
161
  if 'cash_flow' in wb.sheetnames:
162
  ws = wb['cash_flow']
163
  highlight_labels = {
164
+ "cash flow from operations:", "'= net cash from operations",
165
+ "cash flow from investing:", "'= net cash from investing",
166
+ "cash flow from financing:", "'= net cash from financing",
167
+ "net increase/(decrease) in cash", "'= cash at end of period"
 
 
 
 
168
  }
169
  for row in ws.iter_rows():
170
  first_cell = row[0]
 
195
  cell.fill = fill
196
  cell.font = bold
197
 
198
+ wb.save(tmp_path)
199
+ return tmp_path
 
 
200
 
201
  # Gradio wrapper
202
  def extract_and_download(uploaded_file):
203
+ filename = uploaded_file.name if hasattr(uploaded_file, "name") else "output"
204
  if hasattr(uploaded_file, "read"):
205
  data = uploaded_file.read()
206
  else:
207
  with open(uploaded_file, "rb") as f:
208
  data = f.read()
209
+ return process_excel(data, filename)
 
 
 
 
210
 
211
  iface = gr.Interface(
212
  fn=extract_and_download,