Bulk transaction move

Sébastien de Menten sdementen at gmail.com
Sun Dec 28 17:33:16 EST 2014


On Sunday, December 28, 2014, Wm <wm+gnc at tarrcity.demon.co.uk> wrote:

> Sun, 28 Dec 2014 10:16:30 <54A048FE.80909 at polywatt.com>  Thomas Young <
> thomas at polywatt.com>
>
>  Hi Sebastien
>>
>
>  Many many thanks for the good advice and help. Not being up to snuff on
>> Python or GC internals I went out and purchased a book on Python and did
>> a little studying.
>>
>
> I'm impressed.  Stick around, please.
>
>  I then executed your script with mostly good results
>> saving myself a great deal of tedious effort. So far I have been unable
>> to find any negative consequences to my database, so I have been quite
>>
> happy for several days now. Not only did I save myself a lot of effort,
> I have been learning a few things as well.
>
> Learning is fun.
>
> The mysterious aspect to the
> code execution is that i had to run it about 5 times on my database
> before it transferred all the target transactions. It would do many but
> not all on each pass.
>
> I'm going to guess Sebastien hasn't thought all of the scope things
> through.



> Here is my slightly modified version of your script.
>
> import piecash
> from piecash import open_book
> with open_book("test.gnucash",readonly=False) as s:
>      misc_acc = s.accounts.get(name="Miscellaneous")
>      new_acc = s.accounts.get(name="TEMP")
>      for spl in misc_acc.splits:
>          if spl.transaction.description == "Cash":
>              spl.account = new_acc
>      s.save()
>
>
> That wouldn't work here as my end of account tree names aren't unique and
> I use the same description for more than one thing.

One can also use the full name of the account to disambiguate
new_acc = s.accounts.get(fullname="asset:my bank:current account")


> On 12/24/2014 3:49 AM, Sébastien de Menten wrote:
>
>> Hello Thomas,
>>
>> With the recently released piecash (python interface to gnucash), if
>> your gnucash file is in the sqlite format, you could do this with the
>> following script:
>>
>>     from piecash import open_book
>>
>>     with open_book("sample1.gnucash",readonly=False) as s:
>>         misc_acc = s.accounts.get(name="Miscellaneous")
>>         new_acc = s.accounts.get(name="Other account")
>>         for spl in misc_acc.splits:
>>             if spl.transaction.description = "some descrption":
>>                 spl.account = new_acc
>>         s.save()
>>
>
>
> Maybe I am being thick but doesn't this mean the same transactions get
> changed more often than necessary ?

Hmmm, I would say no as the transaction is not changed, only the account of
the split. But maybe the bug is in looping on the misc_acc.splits while it
is changing at the same when the account of the split is changed.  Thomas,
could you test by changing the for line into :
    for spl in list(misc_acc.splits):
and see if it succeeds in one pass ?

>
> --
> Wm...
>
> _______________________________________________
> gnucash-user mailing list
> gnucash-user at gnucash.org
> 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