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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| import pandas as pd
def read_csv(alipay_path, wechat_path, output_path): try: alipay = pd.read_csv(alipay_path, skiprows=24, encoding='GBK') wechat = pd.read_csv(wechat_path, skiprows=16)
required_columns = ['交易订单号', '交易分类', '交易对方', '对方账号', '商品说明', '收/支', '金额', '收/付款方式', '交易状态', '备注', '交易时间']
if all(col in alipay.columns for col in required_columns): alipay_selected = alipay[required_columns] else: print("alipay.csv 文件缺少必要的列。") raise ValueError("alipay.csv 列不完整")
wechat_columns_map = { '交易单号': '交易订单号', '交易类型': '交易分类', '商品': '商品说明', '金额(元)': '金额', '支付方式': '收/付款方式', '当前状态': '交易状态' } wechat.rename(columns=wechat_columns_map, inplace=True)
wechat_selected = pd.DataFrame(columns=required_columns) for col in wechat.columns: if col in required_columns: wechat_selected[col] = wechat[col] for col in required_columns: if col not in wechat_selected.columns: wechat_selected[col] = "/"
alipay_selected = alipay_selected[alipay_selected['收/支'] != '不计收支'] wechat_selected = wechat_selected[wechat_selected['收/支'] != '/']
wechat_selected['金额'] = wechat_selected['金额'].str.replace(r'[¥¥,]', '', regex=True).astype(float)
alipay_selected['分类'] = '支付宝' wechat_selected['分类'] = '微信'
combined_data = pd.concat([alipay_selected, wechat_selected], ignore_index=True)
combined_data.to_csv(output_path, index=False) print(f"文件已成功保存为 '{output_path}'") except FileNotFoundError: print("文件未找到,请检查文件路径。") except pd.errors.ParserError: print("读取 CSV 文件时出现问题,请检查文件格式或编码。") except Exception as e: print(f"发生错误:{e}")
def generate_markdown(csv_file, output_file): import chardet with open(csv_file, 'rb') as f: result = chardet.detect(f.read()) encoding = result['encoding'] data = pd.read_csv(csv_file, encoding=encoding) data['金额'] = data['金额'].replace({'¥': '', ',': ''}, regex=True).astype(float)
total_expense = data[data['收/支'] == '支出']['金额'].sum() total_income = data[data['收/支'] == '收入']['金额'].sum()
expense_by_transaction = data[data['收/支'] == '支出'].groupby('交易分类')['金额'].sum().sort_values(ascending=False) income_by_transaction = data[data['收/支'] == '收入'].groupby('交易分类')['金额'].sum().sort_values(ascending=False)
total_balance = total_income - total_expense print(f"Total Expense: {total_expense}") print(f"Total Income: {total_income}") print(f"Total Balance: {total_balance}") markdown_content = f"**本月消费总额**:¥{total_expense:.2f} | **本月收入总额**:¥{total_income:.2f} | **本月结余**:¥{total_balance:.2f}\n\n"
markdown_content += "## 消费类型分析 💸\n\n" markdown_content += "以下是各消费交易分类与消费金额:\n\n" markdown_content += "| 交易分类 | 消费金额 |\n" markdown_content += "| ---------- | ---------- |\n" for transaction, amount in expense_by_transaction.items(): markdown_content += f"| {transaction} | ¥{amount:.2f} |\n" markdown_content += "\n### 每个交易分类的详细记录:\n" for transaction in expense_by_transaction.index: markdown_content += f"\n#### {transaction}消费记录 💳\n" transaction_data = data[(data['收/支'] == '支出') & (data['交易分类'] == transaction)] markdown_content += "| 交易对方 | 金额 | 分类 | 交易时间 |\n" markdown_content += "| -------- | ----- | ------ | -------- |\n" for _, row in transaction_data.iterrows(): markdown_content += f"| {row['交易对方']} | ¥{row['金额']:.2f} | {row['分类']} | {row['交易时间']} |\n"
markdown_content += "\n## 收入类型分析 💵\n\n" markdown_content += "以下是各收入交易分类与收入金额:\n\n" markdown_content += "| 交易分类 | 收入金额 |\n" markdown_content += "| ---------- | ---------- |\n" for transaction, amount in income_by_transaction.items(): markdown_content += f"| {transaction} | ¥{amount:.2f} |\n" markdown_content += "\n### 每个交易分类的详细记录:\n" for transaction in income_by_transaction.index: markdown_content += f"\n#### {transaction}收入记录 💼\n" transaction_data = data[(data['收/支'] == '收入') & (data['交易分类'] == transaction)] markdown_content += "| 交易对方 | 金额 | 分类 | 交易时间 |\n" markdown_content += "| -------- | ----- | ------ | -------- |\n" for _, row in transaction_data.iterrows(): markdown_content += f"| {row['交易对方']} | ¥{row['金额']:.2f} | {row['分类']} | {row['交易时间']} |\n"
markdown_content += "\n## 收支明细\n" data_sorted = data.sort_values(by='交易时间') markdown_content += "| 交易分类 | 分类 | 收/支 | 金额 | 交易对方 | 商品说明 | 对方账号 | 收/付款方式 | 交易状态 | 备注 | 交易时间 |\n" markdown_content += "| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |\n" for _, row in data_sorted.iterrows(): markdown_content += f"| {row['交易分类']} | {row['分类']} | {row['收/支']} | ¥{row['金额']:.2f} | {row['交易对方']} | {row['商品说明']} | {row['对方账号']} | {row['收/付款方式']} | {row['交易状态']} | {row['备注']} | {row['交易时间']} |\n" with open(output_file, 'w', encoding='utf-8') as f: f.write(markdown_content)
print(f"Markdown 已成功生成并保存为 '{output_file}'")
read_csv('./bill/alipay_record_20250201_091025.csv', './bill/微信支付账单(20250101-20250201)——【解压密码可在微信支付公众号查看】.csv', './bill/合并账单.csv')
generate_markdown('./bill/合并账单.csv', './bill/账单.md')
|