繁体   English   中英

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

[英]Python and GnuCash: Extract data from GnuCash files

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

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

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

  1. 最近更新的文档除了来自GnuCash wiki的文档
  2. 一些解决方法,例如导出到某种文件格式,其中有一个更成熟的Python库可以读取它。

除了提到的那些,你们可能会有更好的建议。

我发布了piecash,这是一个python接口,用于保存使用SQLAlchemy作为基础的SQL保存的GnuCash书籍( 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"]])

GNUCash 2.4已经发布。

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

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

你在谈论数据文件吗? 从那里wiki看起来它们只是压缩的XML文件。 使用Python,您可以使用gzip模块解压缩它们,然后使用任何可用的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'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'

正如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])

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

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

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

https://github.com/MatzeB/pygnucash

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM