我正在寻找有关如何使用python读取GnuCash文件的信息。我读过这个python-gnucash,它为GnuCash库提供了Python绑定,但它目前需要做大量工作(例如依赖项、头等)。这些说明是针对Linux环境和一个相当老的GnuCash版本(2.0.x)而定制的。我正在运行GnuCash 2.2.9。虽然我可以操作Linux命令行,但我在Windows上运行GnuCash。
我的主要目标是读取(还没有写GnuCash文件的计划),这样我就可以使用matplotlib和wxpython创建自己的可视化动态报告。我还没心情学习计划。
我希望有人能在这方面给我一个好的开始。就我所知的GnuCash和Python而言,我认为可能有人知道以下类型的解决方案:
除了提到的建议外,你们还有更好的建议。
发布于 2010-08-04 13:53:25
你是在说数据文件吗?从这里维基看,它们似乎只是压缩的XML文件。WIth,您可以用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'发布于 2014-11-11 20:52:01
我发布了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)最近的版本还允许将拆分的信息直接提取到熊猫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"]])发布于 2011-02-08 03:19:31
GNUCash 2.4已退出。
可以导出到SQL,所以它比解析XML容易得多。
支持Sqlite、MySQL和PostgreSQL (这太酷了!)
https://stackoverflow.com/questions/3378409
复制相似问题