Python和GnuCash:从GnuCash文件中提取数据

时间:2010-07-31 14:01:54

标签: python api matplotlib

我正在寻找有关如何使用python读取GnuCash文件的信息。我已经阅读了这个python-gnucash,它提供了对GnuCash库的Python绑定,但目前需要做很多工作(例如依赖项,标题等)。这些说明是针对Linux环境而定制的,而且是一个相当古老的GnuCash版本(2.0.x)。我正在运行GnuCash 2.2.9。虽然我可以操作Linux命令行,但我在Windows XP上运行GnuCash。

我的主要目标是阅读(暂无编写)我的GnuCash文件,以便我可以使用matplotlib创建自己的可视化动态报告wxpython。我还没有心情学习Scheme。

我希望有人能指出我这方面的良好开端。据我所知,GnuCash和Python,我认为有人可能知道以下类型的解决方案:

  1. this one from the GnuCash wiki
  2. 之外的最新更新文档
  3. 一些解决方法,例如导出到某种文件格式,其中有一个更成熟的Python库可以读取它。
  4. 除了提到的那些,你们可能会有更好的建议。

6 个答案:

答案 0 :(得分:5)

GNUCash 2.4已经出局。

可以导出到SQL,因此它比解析XML容易得多。

支持Sqlite,MySQL和PostgreSQL(这有多酷!)

答案 1 :(得分:5)

我发布了piecash,一个用于SQL保存的GnuCash书籍的python接口,它使用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)

或迭代“资产”帐户中的所有拆分:

# 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"]])

答案 2 :(得分:4)

您在谈论数据文件吗?从那里wiki,看起来它们只是压缩的XML文件。使用Python,您可以使用gzip module解压缩它们,然后使用available XML parsers中的任何一个解析它们。

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's "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\'s "Foligno" Madonna, painted in\n    '
>>> tree.getchildren()[0].get('src')
'madonna.jpg'

答案 3 :(得分:1)

我在django应用程序中采用了sqlite方法,我编写了类似的东西(虽然用于预算)。有关代码,请参阅https://github.com/evandavey/OpenBudget/blob/master/openbudgetapp/management/commands/gnucash-import.py

就数据本身而言,我使用了pandas库来处理它的时间序列性质。

答案 4 :(得分:1)

正如Chop Suey所说,GnuCash 2.4有自己的数据库格式。如果您仍想使用XML文件,可以使用以下脚本从XML转换为数据库,然后在其上编写报告(例如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])

答案 5 :(得分:0)

我刚刚发布了一些python代码,可以读取和解释gnucash 2.6及更高版本中使用的sqlite3文件格式:

https://github.com/MatzeB/pygnucash