Add script to generate pdf reports.
This commit is contained in:
48
tools/report.sh
Normal file
48
tools/report.sh
Normal file
@@ -0,0 +1,48 @@
|
||||
#!/bin/sh
|
||||
|
||||
CSV_FILE="data.csv"
|
||||
REPORT_MD="report.md"
|
||||
REPORT_PDF="/home/felixm/report.pdf"
|
||||
YEAR="2018-04"
|
||||
HLEDGER="hledger balance -M -V -b $YEAR --depth 1 --transpose -o $CSV_FILE"
|
||||
|
||||
# Networth
|
||||
$HLEDGER -H '^assets'
|
||||
sed -i 's/\$//g' $CSV_FILE
|
||||
python tofig.py $CSV_FILE "figure_1.png"
|
||||
|
||||
$HLEDGER '^assets'
|
||||
sed -i 's/\$//g' $CSV_FILE
|
||||
sed -i 's/assets/cashflow/g' $CSV_FILE
|
||||
python tofig.py $CSV_FILE "figure_2.png" --type bar
|
||||
|
||||
$HLEDGER '^expenses'
|
||||
sed -i 's/\$//g' $CSV_FILE
|
||||
python tofig.py $CSV_FILE "figure_3.png" --type bar
|
||||
|
||||
$HLEDGER '^income'
|
||||
sed -i 's/\$-//g' $CSV_FILE
|
||||
python tofig.py $CSV_FILE "figure_4.png" --type bar
|
||||
|
||||
rm $CSV_FILE
|
||||
|
||||
cat > $REPORT_MD <<- EOM
|
||||
# Financial Report
|
||||
|
||||
# Networth
|
||||

|
||||
|
||||
# Cashflow
|
||||

|
||||
|
||||
# Expenses
|
||||

|
||||
|
||||
# Income
|
||||

|
||||
EOM
|
||||
|
||||
pandoc $REPORT_MD -o $REPORT_PDF
|
||||
rm figure_*
|
||||
rm $REPORT_MD
|
||||
|
||||
40
tools/tofig.py
Normal file
40
tools/tofig.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
import argparse
|
||||
|
||||
|
||||
def line_format(label):
|
||||
"""
|
||||
Convert time label to the format of pandas line plot
|
||||
"""
|
||||
month = label.month_name()[:3]
|
||||
if month == 'Jan':
|
||||
month = f'{label.year}\n{month}'
|
||||
return month
|
||||
|
||||
|
||||
def create_figure(args):
|
||||
df = pd.read_csv(args.csv_file, index_col='Account', parse_dates=True)
|
||||
df = df.drop(columns="Total:")
|
||||
figsize=(10, 6)
|
||||
if args.type == 'bar':
|
||||
ax = df.plot(grid=True, kind='bar', figsize=figsize)
|
||||
ax.set_xticklabels(map(lambda x: line_format(x), df.index))
|
||||
else:
|
||||
ax = df.plot(grid=True, figsize=figsize)
|
||||
plt.savefig(args.png_file)
|
||||
# plt.show()
|
||||
|
||||
|
||||
def get_args():
|
||||
parser = argparse.ArgumentParser(description='Transform a CSV file into a bar chart.')
|
||||
parser.add_argument('csv_file', type=str, help='CSV data file')
|
||||
parser.add_argument('png_file', type=str, help='Chart png file')
|
||||
parser.add_argument('--type', default='', type=str, help='Chart png file')
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
args = get_args()
|
||||
create_figure(args)
|
||||
|
||||
Reference in New Issue
Block a user