银行账单(建行/招商)转换beancount
本帖最后由 平Fan_d世界 于 2023-11-3 19:48 编辑用了beancount来记账后,发现每月的账单手动记是一件极其麻烦的事情。
然后再github搜索一通后,有double-entry-generator(https://github.com/deb-sig/double-entry-generator)能转换支付宝/微信的账单,但是没有自己用的建行和招商。
所有自己写了一个py:
[*]建行能导出的账单是excel格式的直接用;
[*]招商导出的是pdf,幸好还能转成txt,然后自己整理成csv
[*]后续可能会加上深农商和农行,也可能不会,这两张卡用的不多。
代码如下:
import os
import re
import pandas as pd
import numpy as np
dir = os.path.dirname(os.path.abspath(__file__))
#input_file = "xxxxxx.csv"
input_file = input("输入账单路径:")
bank = input("输入账单归属银行:") #暂时只有CCB和CMB
output_file = dir + "\\" + bank + ".bean"
config_file = dir + "\\config\\" + bank + ".conf" #当前config文件夹下的CCB.conf和CMB.conf。
#CCB账单excel的整理
def CCB_data(_file_):
data = pd.read_excel(_file_)
data.columns = ["No","text0","CCY","Yuan","time","cost","bal","text1","text2"]
data = data.iloc
#data["time"] = data["time"].str + "-" + data["time"].str + "-" + data["time"].str
data["time"] = pd.to_datetime(data["time"]).astype(str)
data.loc[:,"account"] = data["text0"] + ":" + data["text2"]
data["CCY"] = data["CCY"].replace("人民币元","CNY")
data = data[["time","cost","CCY","account","text0","text1","text2"]]
#pd.DataFrame(data).to_csv(output_file+".csv",encoding='utf-8-sig') #可以导出csv
return data
#CMB账单csv的整理
def CMB_data(_file_):
data = pd.read_csv(_file_)
data.columns = ["time","CCY","cost","bal","text0","text2"]
data["time"] = pd.to_datetime(data["time"]).astype(str)
data["cost"] = data["cost"].astype(str)
data.loc[:,"text1"] = None
data.loc[:,"account"] = data["text0"] + ":" + data["text2"]
data = data[["time","cost","CCY","account","text0","text1","text2"]]
#pd.DataFrame(data).to_csv(output_file+".csv",encoding='utf-8-sig') #可以导出csv
return data
#关键字查找替换
def replace(_data_,_config_):
conf = pd.read_csv(_config_)
for conf in conf.itertuples():
_data_ = _data_.str.replace(".*"+conf+".*",conf,regex = True)
return _data_
#输出beancount格式的字段
def to_bean(_data_):
global bank
bean = _data_["time"] + ' * \"' +_data_["text0"] + '\"\n\t'
if _data_["text1"].notnull().any():
bean = bean + 'notes: \"' + _data_["text1"] + '\"\n\t'
bean = bean + 'id: \"' + _data_["text2"] + '\"\n\t'
bean = bean + "Assets:Bank:" + bank + " " + _data_["cost"] + " " + _data_["CCY"] + "\n\t"
bean = bean + _data_["account"] + "\n"
return bean
#整理账单格式
if bank == "CCB":
DATA = CCB_data(input_file)
if bank == "CMB":
DATA = CMB_data(input_file)
#按conf文件查找替换关键字
DATA["account"]= replace(DATA["account"],config_file)
#print(DATA)
#整理为beancount格式的字段
bean = to_bean(DATA).to_frame()
#print(bean)
#输出文件CCB.bean或者CMB.beantext = ""
for bean in bean.itertuples():
text = text + str(bean) + "\n"
file = open(output_file,'w')
file.write(text)
file.close()
config文件,格式:【关键字,替换后】,例如:
Text,Replace_text
饿了么,Expenses:Food:Others
效果如下:
https://i2.100024.xyz/2023/11/03/ul29rw.webp
https://i2.100024.xyz/2023/11/03/um3c8h.webp
感谢分享! 请做成成品,谢谢! chayunyuxiang 发表于 2023-11-3 19:16
请做成成品,谢谢!
没那技术,我是新手,谢谢{:301_992:} 感谢楼主的成品,膜拜大佬 银行流水等表格类表单,pdf或者jpg格式,有啥办法批量orc转换为excel或者csv? sai609 发表于 2023-11-3 21:37
银行流水等表格类表单,pdf或者jpg格式,有啥办法批量orc转换为excel或者csv?
木的办法,就算ocr设别准确率高,格式也会乱,还是得靠人工检查 平Fan_d世界 发表于 2023-11-3 22:00
木的办法,就算ocr设别准确率高,格式也会乱,还是得靠人工检查
格式是一大问题
个别数字,与上下连接页码,经常混杂没法ocr sai609 发表于 2023-11-4 10:21
格式是一大问题
个别数字,与上下连接页码,经常混杂没法ocr
如果是自用的话,可以做一些简单的格式区分,手工调整的情况并不多。但是没法作为成品发布 你把pdf的转换加到pythin里,应该更好使吧
页:
[1]
2