FinancialAnalyzer / scripts /financial_utils.py
Shivam4096's picture
Upload folder using huggingface_hub
3d13a56 verified
Raw History Blame Contribute Delete
1.17 kB
import pandas as pd
import matplotlib.pyplot as plt
import io
from docx import Document
from docx.shared import Inches
def format_indian_units(value):
"""Converts a number to Crores or Lakhs string."""
try:
val = float(value)
if abs(val) >= 10**7:
return f"{val / 10**7:.2f} Cr"
elif abs(val) >= 10**5:
return f"{val / 10**5:.2f} L"
else:
return f"{val:,.2f}"
except (ValueError, TypeError):
return value
def create_styled_chart(df, title, ylabel, filename):
"""Creates a professional financial chart."""
plt.figure(figsize=(10, 6))
plt.style.use('ggplot')
df.plot(kind='bar', ax=plt.gca())
plt.title(title, fontsize=14, fontweight='bold')
plt.ylabel(ylabel)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig(filename, dpi=300)
plt.close()
def add_paragraph_with_insights(doc, heading, content):
"""Adds a heading and a multi-paragraph insight block."""
doc.add_heading(heading, level=2)
doc.add_paragraph(content)
# This script can be imported or used as a template by the agent.