[GNC] Problem getting child accounts with Python
Ross Reedstrom
ross at reedstrom.org
Thu Oct 3 12:58:03 EDT 2024
I tried your example code, and I'm sorry to say "it works for me". Took a
bit to extract it from the email (NBSP is not my friend) but here's what I
see when running it locally:
$ python3 m.py
Root
Expenses
I'm assuming you get an error of some sort?
If I import the create_accounts() method into an interactive session, (and
open up a session of my own) I see:
>>> root_acct.get_children()
[<gnucash.gnucash_core.Account object at 0x7fb778446120>,
<gnucash.gnucash_core.Account object at 0x7fb778444da0>,
<gnucash.gnucash_core.Account object at 0x7fb77804d9a0>]
>>> [ca.name for ca in root_acct.get_children()]
['Expenses', 'Savings', 'Opening Balance']
>>>
This is on Ubuntu 24.04.1, using system installed gnucash and
python3-gnucash packages.
Ross
On Sat, Sep 28, 2024 at 3:43 PM Eberhard Beilharz <eb1 at sil.org> wrote:
> Hi,
>
> I'm trying to create a Python script with the GnuCash API. I started
> with the `simple_test.py` sample and extended that a bit (see below).
> However, I ran into a problem: after re-opening the session and getting
> the root account, it doesn't show any child accounts. I tried different
> ways, but no luck. I guess that I'm missing something, but I don't know
> what because all the examples I could find do it the same way, and
> things work when newly creating the file and accounts.
>
> Any ideas?
>
> Thanks,
> Eberhard
>
> GnuCash 5.8 built from git
>
> #!/usr/bin/env python3
>
> from gnucash import (
> Session, Account, Transaction, Split, GncNumeric, SessionOpenMode)
>
> FILE_1 = "/tmp/example3.gnucash"
>
> def create_accounts():
> with Session("xml://%s" % FILE_1,
> SessionOpenMode.SESSION_NEW_OVERWRITE) as session:
>
> book = session.book
> root_acct = Account(book)
> expenses_acct = Account(book)
> savings_acct = Account(book)
> opening_acct = Account(book)
> trans1 = Transaction(book)
> trans1.BeginEdit()
> trans2 = Transaction(book)
> trans2.BeginEdit()
>
> split1 = Split(book)
> split3 = Split(book)
> comm_table = book.get_table()
> cad = comm_table.lookup("CURRENCY", "CAD")
>
> num1 = GncNumeric(4, 1)
> num2 = GncNumeric(100, 1)
>
> #Set new root account
> book.set_root_account(root_acct)
>
> #Set up root account and add sub-accounts
> root_acct.SetName("Root")
> root_acct.SetType(13) #ACCT_TYPE_ROOT = 13
> root_acct.append_child(expenses_acct)
> root_acct.append_child(savings_acct)
> root_acct.append_child(opening_acct)
>
> #Set up Expenses account
> expenses_acct.SetCommodity(cad)
> expenses_acct.SetName("Expenses")
> expenses_acct.SetType(9) #ACCT_TYPE_EXPENSE = 9
>
> #Set up Savings account
> savings_acct.SetCommodity(cad)
> savings_acct.SetName("Savings")
> savings_acct.SetType(0) #ACCT_TYPE_BANK = 0
>
> #Set up Opening Balance account
> opening_acct.SetCommodity(cad)
> opening_acct.SetName("Opening Balance")
> opening_acct.SetType(10) #ACCT_TYPE_EQUITY = 10
>
> split1.SetValue(num1)
> split1.SetAccount(expenses_acct)
> split1.SetParent(trans1)
>
> split3.SetValue(num2)
> split3.SetAccount(savings_acct)
> split3.SetParent(trans2)
>
> trans1.SetCurrency(cad)
> trans1.SetDate(14, 3, 2006)
> trans1.SetDescription("Groceries")
>
> trans2.SetCurrency(cad)
> trans2.SetDate(7, 11, 1995)
> trans2.SetDescription("Opening Savings Balance")
>
> split2 = Split(book)
> split2.SetAccount(savings_acct)
> split2.SetParent(trans1)
> split2.SetValue(num1.neg())
>
> split4 = Split(book)
> split4.SetAccount(opening_acct)
> split4.SetParent(trans2)
> split4.SetValue(num2.neg())
>
> trans1.CommitEdit()
> trans2.CommitEdit()
> session.save()
> session.end()
>
>
> def read_accounts():
> with Session("xml://%s" % FILE_1,
> SessionOpenMode.SESSION_READ_ONLY) as session:
> book = session.book
> root_acct = book.get_root_account()
> print(root_acct.name)
> expenses_acct = root_acct.nth_child(0)
> print(expenses_acct.name)
> savings_acct = root_acct.lookup_by_name("Savings")
>
>
> create_accounts()
> read_accounts()
>
>
> _______________________________________________
> gnucash-user mailing list
> gnucash-user at gnucash.org
> To update your subscription preferences or to unsubscribe:
> https://lists.gnucash.org/mailman/listinfo/gnucash-user
> -----
> Please remember to CC this list on all your replies.
> You can do this by using Reply-To-List or Reply-All.
>
More information about the gnucash-user
mailing list