Skip to content

Commit c25f170

Browse files
add: credit card statement analytics
Fixes #1
1 parent d22b616 commit c25f170

File tree

6 files changed

+70
-11
lines changed

6 files changed

+70
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Get analysis of HDFC bank account and credit card statement
1414

1515
1. Install CLI
1616
```
17-
pip install https://github.com/vipul-sharma20/hdfc-analytics/releases/download/v0.2.0/hdfc_analytics-0.2.0-py3-none-any.whl
17+
pip install https://github.com/vipul-sharma20/hdfc-analytics/releases/download/v0.3.0/hdfc_analytics-0.3.0-py3-none-any.whl
1818
```
1919
(check releases for latest whl releases)
2020

configs/column_mapping.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
[default]
1+
[account]
22
date = "Date"
33
description = "Narration"
44
reference = "Chq./Ref.No."
55
value_date = "Value Dt"
66
withdrawal_amount = "Withdrawal Amt."
77
deposit_amount = "Deposit Amt."
88
closing_balance = "Closing Balance"
9+
10+
[cc]
11+
date="date"
12+
description="description"
13+
rp="rp"
14+
amount="amount"

hdfc_analytics/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.0'
1+
__version__ = '0.3.0'

hdfc_analytics/cli.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
44
Usage:
55
hdfc-analytics account --statement-csv=<statement-csv> --categories-config=<categories-config-toml> --column-config=<column-mapping-toml> [--llm=<llm-name>] [--llm-host=<llm-host-url>]
6+
hdfc-analytics cc --statement-dir=<statement-dir> --name=<name> --password=<password> --categories-config=<categories-config-toml> --column-config=<column-mapping-toml> [--llm=<llm-name>] [--llm-host=<llm-host-url>]
67
78
Options:
8-
--statement-csv=<sattement-csv> Path to bank account / credit card statement csv.
9+
--statement-csv=<sattement-csv> Path to bank account statement csv.
10+
--statement-dir=<sattement-dir> Path to bank credit card statement directory.
11+
--name=<name> Your name as written on the statement (applicable for CC analytics only)
12+
--password=<password> Password to open CC statement pdf (format is: first 5 letters of your first name followed by DDMM of your DOB)
913
--categories-config=<categories-config-toml> Path to file with categories configs.
1014
--column-config=<column-mapping-toml> Path to file with column mapping configs.
1115
--llm=<llm-name> Flag to enable LLMs to tag transaction.
1216
--llm-host=<llm-host-url> LLM host. Applicable for Ollama or Huggingface served models.
1317
"""
1418

19+
import glob
20+
import os
1521
from typing import List
1622

23+
import hdfc_cc_parser
1724
import pandas as pd
1825
import toml
1926
from docopt import docopt
@@ -49,12 +56,44 @@ def main():
4956

5057
plot_df(categorized_df)
5158

59+
if args["cc"]:
60+
statement_dir = args["--statement-dir"]
61+
categories_config = args["--categories-config"]
62+
column_config = args["--column-config"]
63+
llm_host = args["--llm-host"]
64+
llm = args["--llm"]
65+
66+
pdf_files = glob.glob(os.path.join(statement_dir, "*.PDF"))
67+
dfs = []
68+
for pdf_file in pdf_files:
69+
output = hdfc_cc_parser.parse_cc_statement(pdf_file, args["--name"], args["--password"])
70+
df = pd.DataFrame([row.split(',') for row in output.split('\n') if row], columns=['date', 'description', 'rp', 'amount'])
71+
dfs.append(df)
72+
73+
# Concatenate all the DataFrames
74+
combined_df = pd.concat(dfs, ignore_index=True)
75+
76+
# Load the mappings
77+
column_mappings = load_column_mappings(column_config, statement_type="cc")
78+
79+
# Apply the column mappings
80+
combined_df = map_columns(combined_df, column_mappings)
81+
82+
categorizer = StatementCategorizer(categories_config, llm_host, llm)
83+
84+
# Categorize the DataFrame
85+
categorized_df = categorizer.categorize_dataframe(combined_df)
86+
87+
plot_df(categorized_df, statement_type="cc")
88+
5289

5390
# Load column mappings from a TOML file
54-
def load_column_mappings(config_path: str) -> List[str]:
91+
def load_column_mappings(
92+
config_path: str, statement_type: str = "account"
93+
) -> List[str]:
5594
with open(config_path, "r") as config_file:
5695
config = toml.load(config_file)
57-
return config["default"] # Assuming there"s only one mapping set, "default"
96+
return config[statement_type] # Assuming there"s only one mapping set, "default"
5897

5998

6099
# Apply the column mappings to a DataFrame

hdfc_analytics/plot.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@
22
import plotly.express as px
33

44

5-
def plot_df(categorized_df: pd.DataFrame) -> None:
5+
def plot_df(categorized_df: pd.DataFrame, statement_type: str = "account") -> None:
6+
if statement_type == "cc":
7+
categorized_df["amount"] = categorized_df["amount"].apply(
8+
lambda x: abs(float(x)) if float(x) < 0 else 0
9+
)
10+
else:
11+
categorized_df["amount"] = categorized_df["withdrawal_amount"].fillna(0)
612

7-
categorized_df["amount"] = categorized_df["withdrawal_amount"].fillna(0)
13+
# Filter out positive amounts
14+
if statement_type == "cc":
15+
categorized_df = categorized_df[categorized_df["amount"] > 0]
816

917
# Summarize the data by category
1018
category_summary = categorized_df.groupby("category")["amount"].sum().reset_index()
1119

1220
# Plot the pie chart using Plotly
13-
fig = px.pie(category_summary, values="amount", names="category", title="Expenses by Category")
14-
fig.update_traces(textinfo='percent+label')
21+
fig = px.pie(
22+
category_summary,
23+
values="amount",
24+
names="category",
25+
title="Expenses by Category",
26+
)
27+
fig.update_traces(textinfo="percent+label")
1528

1629
# Show the figure
1730
fig.show()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "hdfc-analytics"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "HDFC statement analysis"
55
authors = ["Vipul Sharma <vipul.sharma20@gmail.com>"]
66
readme = "README.md"
@@ -12,6 +12,7 @@ toml = "^0.10.2"
1212
docopt = "^0.6.2"
1313
plotly = "^5.18.0"
1414
litellm = "^1.61.6"
15+
hdfc-cc-parser-rs = "^0.0.7"
1516

1617
[tool.poetry.scripts]
1718
hdfc-analytics = "hdfc_analytics.cli:main"

0 commit comments

Comments
 (0)