我使用 beancount 记账流程中的有一步是这样的,将每张信用卡的账单日添加到滴答清单中,每月重复。
每当到达账单日时,提醒我在 beancount 文件中添加一条对应帐目的 balance 断言语句,由于大部分信用卡
的出账是有滞后的,所以该balance断言通常需要同时加上!标记,便于后续账单出来后修改。
这个过程是比较机械的,所以我想到了使用 chatGPT 来自动化这个过程,这样我只需要在出账后手动更新自动添加
的断言语句,将金额修改为正确的还款金额,同时去除!标记。最终的脚本如下。
import datetime
def add_balance_statement(ledger_path, account, date, balance, currency):
with open(ledger_path, 'a') as file:
file.write(f"{date} balance {account} {balance} {currency}\n")
def auto_add_balance_statements(ledger_path, credit_cards):
today = datetime.date.today()
for card in credit_cards:
bill_date = card['bill_date']
accounts = card['accounts']
balance = card['balance']
currency = card['currency']
if today.day == bill_date:
for account in accounts:
add_balance_statement(ledger_path, account, today, balance, currency)
print(f"Balance statement added for {account} on {today}.")
# Specify the path to your Beancount ledger file
ledger_file_path = "<ledger_path>"
# Specify your credit card details (bill date, accounts, balance, and currency)
credit_cards = [
{
'bill_date': 1, # Example: 1st of the month
'accounts': [
{'account': 'Assets:CreditCards:Card1:Account1', 'currency': 'USD'}, # Example: Account1 for Card1 with USD currency
{'account': 'Assets:CreditCards:Card1:Account2', 'currency': 'EUR'}, # Example: Account2 for Card1 with EUR currency
],
'balance': 1000.00, # Example: Current balance for Card1
'currency': 'USD' # Example: Currency for Card1
},
{
'bill_date': 15, # Example: 15th of the month
'accounts': [
{'account': 'Assets:CreditCards:Card2:Account1', 'currency': 'USD'}, # Example: Account1 for Card2 with USD currency
{'account': 'Assets:CreditCards:Card2:Account2', 'currency': 'GBP'}, # Example: Account2 for Card2 with GBP currency
],
'balance': 2000.00, # Example: Current balance for Card2
'currency': 'USD' # Example: Currency for Card2
},
# Add more credit card details as needed
]
# Call the function to automatically add balance statements
auto_add_balance_statements(ledger_file_path, credit_cards)
对于有兴趣的读者,可以看我和 chatgpt 的几轮对话。
...