#!/usr/bin/env python3 # -*- coding: utf-8 -*- # This program is Copyright (C) 2017, Paul Lutus # and is released under the GPL: # https://www.gnu.org/licenses/gpl-3.0.en.html import sys, re from piecash import open_book, Account, GnucashException # change this path to suit your own needs data_path = '/netbackup/temp/all_accounts.sqlite3.gnucash' reply = input("Have you made a backup of your GnuCash database (y/n): ") if reply != "y": print("Always make a backup of your GnuCash database before using this program.") quit() # think it over before changing this read_only = True try: with open_book(data_path,readonly = read_only) as book: from_acct = book.root_account.children(name="Assets").children(name="Current Credit Card") to_acct_name = "Credit Card Payments" try: to_acct = book.root_account.children(name=to_acct_name) except KeyError as err: # create a nonexistent account print('Creating new account "%s" ...' % to_acct_name) to_acct = Account(name=to_acct_name, type="BANK", parent=book.root_account, commodity=book.commodities.get(mnemonic="USD"), ) print('Accounts: From:"%s" -> To:"%s"' % (from_acct,to_acct)) total = 0 for transaction in book.transactions: for split in transaction.splits: if split.account == from_acct and re.search('PAYMENT DIRECT',transaction.description): print("\nTransaction: %s" % str(transaction)) print(" Split: %s" % str(split)) split.account = to_acct total += 1 else: sys.stdout.write(".") sys.stdout.flush() if total > 0: reply = input("\nEdited %d transactions, okay to commit (y/N)? " % total) if reply == 'y': book.save() else: print('\nNo transactions changed.') except GnucashException as err: print('Error: "%s"' % err)