File size: 1,682 Bytes
1d54def |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import matplotlib.pyplot as plt
import pandas as pd
import io
import base64
def generate_report(df, spending_analysis, recommendations):
report = f"""
## Invoice Reader & Budget Categorizer Report
### Categorized Transactions
{df.to_markdown(index=False)}
### Spending Insights
{spending_analysis}
### Budget Recommendations
{recommendations}
"""
# Generate visualizations
if not df.empty:
# Category-wise spending pie chart
category_spending = df.groupby("category")["amount"].sum()
plt.figure(figsize=(6, 6))
plt.pie(category_spending, labels=category_spending.index, autopct="%1.1f%%")
plt.title("Category-wise Spending")
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
category_plot = base64.b64encode(buf.getvalue()).decode("utf-8")
plt.close()
# Monthly spending trend
monthly_spending = df.groupby("month")["amount"].sum()
plt.figure(figsize=(8, 4))
monthly_spending.plot(kind="line", marker="o")
plt.title("Monthly Spending Trend")
plt.xlabel("Month")
plt.ylabel("Amount")
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
monthly_plot = base64.b64encode(buf.getvalue()).decode("utf-8")
plt.close()
report += f"""
### Visualizations
#### Category-wise Spending

#### Monthly Spending Trend

"""
return report |