Python and GnuCash: Extracting Data from GnuCash Files

I am looking for information on how to read GnuCash files using python. I read about this python-gnucash, which provides Python bindings to the GnuCash library, but it needs a lot of work at the moment (like dependencies, headers, etc.). The instructions are for the Linux environment and the rather old version of GnuCash (2.0.x). I am running GnuCash 2.2.9. Although I can manage the Linux command line, I run GnuCash on Windows XP.

My main goal is to read (until you plan to write) my GnuCash files so that I can create my own visual dynamic reports using matplotliband wxpython. I am not yet in the mood to study the Scheme.

Hope someone can direct me to a good start. As far as I know about GnuCash and Python, I think that someone probably knows the solutions of the following types:

  • Recently updated documentation other than this from the GnuCash wiki
  • Some workaround, for example, exporting to a specific file format, for which there is a more mature Python library that can read it.

You guys may have better deals in addition to those mentioned.

+3
source share
6 answers

? wiki, , XML . WIth Python, gzip module, XML.

ElementTree

>>> import xml.etree.cElementTree as ET
>>> xmlStr = '''<?xml version="1.0" encoding="UTF-8" ?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael "Foligno" Madonna, painted in
     <date>1511</date>?<date>1512</date>.
</caption>
</painting>
'''
>>> tree = ET.fromstring(xmlStr)  #use parse or iterparse to read direct from file path
>>> tree.getchildren()
[<Element 'img' at 0x115efc0>, <Element 'caption' at 0x1173090>]
>>> tree.getchildren()[1].text
'This is Raphael\ "Foligno" Madonna, painted in\n    '
>>> tree.getchildren()[0].get('src')
'madonna.jpg'
+4

GNUCash 2.4 .

SQL, , XML.

Sqlite, MySQL PostgreSQL ( !)

+5

piecash, python SQL, GnuCash, SQLAlchemy (https://github.com/sdementen/piecash).

, .

, :

from piecash import open_book

# open a book
with open_book("some_book.gnucash", open_if_lock=True) as mybook:
    # iterate over all accounts of the book
    for account in mybook.accounts:
        print(account)

"Asset":

# open the book
with open_book("some_book.gnucash", open_if_lock=True) as mybook:
    # retrieve the account by its fullname
    asset = mybook.accounts(fullname="Asset")
    # iterate over all its splits
    for split in asset.splits:
        print(split)

pandas DataFrames /

from piecash import open_book

# open a book
with open_book("some_book.gnucash", open_if_lock=True) as mybook:
    # extract all split information to a pandas DataFrame
    df = mybook.splits_df()

    # print for account "Asset" some information on the splits
    print(df.loc[df["account.fullname"] == "Asset",
                 ["transaction.post_date", "value"]])
+5

As Chop Sui said, GnuCash 2.4 has its own database format. If you still want to use XML files, you can use the following script to convert from XML to a database, and then write your reports (e.g. gnucashconvert filename.gnucash sqlite3: ////home/username/export.sqlite):

#!/usr/bin/env python

import os
import gnucash

def convert_gnucash(src_uri, target_uri):
    """Converts gnucash databases at the given uris from src to target"""
    session = gnucash.Session(src_uri)
    try:
        new_session = gnucash.Session(target_uri, is_new=True)
        try:
            new_session.swap_data(session)
            new_session.save()
        finally:
            new_session.end()
            new_session.destroy()
    finally:
        session.end()
        session.destroy()

if __name__ == "__main__":
    import sys
    if len(sys.argv) > 2:
        src_uri, target_uri = sys.argv[1], sys.argv[2]
        src_uri = ("xml://%s" % os.path.abspath(src_uri) if "://" not in src_uri else src_uri)
        target_uri = ("xml://%s" % os.path.abspath(target_uri) if "://" not in target_uri else target_uri)
        convert_gnucash(src_uri, target_uri)
    else:
        print >>sys.stderr, "Syntax %s src target" % (sys.argv[0])
+1
source

I just published python code that can read and interpret the sqlite3 file format used in gnucash 2.6 and above:

https://github.com/MatzeB/pygnucash

0
source

Source: https://habr.com/ru/post/1757462/


All Articles