Migration - Importing transactions via python binding

Philip Tait philip at taits.org
Sun Aug 25 01:29:41 EDT 2013


While looking around for helpful Python Gnucash examples, I ran across a
message posted to this list by lukasz.infobox at gmail.com on 2013-01-08.

His script helped fill-in some of the gaps for me, and I got a working
script (and found why his script didn't work), so I'm posting my working
example here, it case it helps anyone.

'data1' is an existing XML data file.

This script does cause the infamous "<GLib> g_hash_table_foreach: assertion
`version == hash_table->version' failed" message, but discussion elsewhere
seems to indicate this is benign.

-----------------------------------------------------------

from gnucash import Session, Account, Transaction, Split, GncNumeric

session = Session("data1")
book = session.book
root_acct = book.get_root_account()
comm_table = book.get_table()
usd = comm_table.lookup("CURRENCY", "USD")
acct1 = root_acct.lookup_by_name("MC XXXXb")
acct2 = root_acct.lookup_by_name("Groceries")
trans1 = Transaction(book)
trans1.BeginEdit()
num1 = GncNumeric(4, 1)
trans1.SetCurrency(usd)
trans1.SetDate(8, 1, 2013)
trans1.SetDescription("Food")
split1 = Split(book)
split1.SetValue(num1)
split1.SetAccount(acct1)
split1.SetParent(trans1)

split2 = Split(book)
split2.SetValue(num1.neg())
split2.SetAccount(acct2)
split2.SetParent(trans1)
trans1.CommitEdit()
session.save()

session.end()
session.destroy()


More information about the gnucash-user mailing list