Python 和 GnuCash:从 GnuCash 文件中提取数据
我正在寻找有关如何使用 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 的了解,我认为有人可能知道以下类型的解决方案:
- 除了 这个来自 GnuCash wiki
- 一些解决方法,比如导出为某种文件格式,有更成熟的 Python 库可以读取它。
除了上面提到的之外,你们可能还有更好的建议。
I'm looking for information on how to read GnuCash files using python. I have read about this python-gnucash
which provides Python bindings to the GnuCash library, but it takes a lot of work at the moment (e.g. dependencies, headers, etc.). The instructions are tailored for the Linux environment, and a rather old GnuCash version (2.0.x). I am running GnuCash 2.2.9. Though I can operate the Linux command line, I am running GnuCash on Windows XP.
My main objective is to read (no plans to write yet) my GnuCash files so that I can create my own visual dynamic reports using matplotlib
and wxpython
. I'm not yet in the mood to learn Scheme.
I hope someone can point me to a good start on this. As far as I know about GnuCash and Python, I think someone probably knows solutions of the following types:
- More recently updated documentation aside from this one from the GnuCash wiki
- Some workaround, like exporting to a certain file format for which there is a more mature Python library that can read it.
You guys might have better suggestions in addition to those mentioned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发布了 piecash,这是一个 SQL 保存的 GnuCash 书籍的 Python 接口,它使用 SQLAlchemy 作为基础(https://github.com/sdementen /piecash)。
有了它,您可以轻松访问书中包含的所有信息。
例如,迭代书中的所有帐户:
或迭代“资产”帐户中的所有拆分:
最近的版本还允许将拆分信息直接提取到 pandas DataFrames,以便使用以下命令轻松绘制/分析
I published piecash, a python interface to SQL saved GnuCash books that uses SQLAlchemy as basis (https://github.com/sdementen/piecash).
With it you can easily access all the information contained in a book.
For instance, to iterate over all accounts in the book:
or to iterate over all the splits in the "Asset" account:
Recent versions also allows to extract the split information directly to pandas DataFrames for easy plotting/analysis with
GNUCash 2.4 已发布。
可以导出到 SQL,因此比解析 XML 容易得多。
支持 Sqlite、MySQL 和 PostgreSQL(这太酷了!)
GNUCash 2.4 is out.
Can export to SQL so it's very much easier than parsing XML.
Sqlite, MySQL and PostgreSQL are supported (how cool is that!)
你是在谈论数据文件吗?从 wiki 看来,它们只是压缩的 XML 文件。使用 Python,您可以使用 gzip 模块 解压缩它们,然后使用以下任意命令解析它们: 可用的 XML 解析器。
元素树示例
Are you talking about the data files? From there wiki, it looks like they are just compressed XML files. WIth Python, you can decompress them with the gzip module and then parse them with any of the available XML parsers.
ElementTree Example
我在我编写的 django 应用程序中采用了 sqlite 方法来做类似的事情(尽管是为了预算)。请参阅 https://github.com/evandavey/ OpenBudget/blob/master/openbudgetapp/management/commands/gnucash-import.py 获取代码。
就数据本身而言,我使用 pandas 库来处理其时间序列性质。
I've taken the sqlite approach in a django app I've written to do a similar thing (although for budgeting). See https://github.com/evandavey/OpenBudget/blob/master/openbudgetapp/management/commands/gnucash-import.py for the code.
In terms of the data itself, I've used the pandas library to handle its time series nature.
正如 Chop Suey 所说,GnuCash 2.4 有自己的数据库格式。如果您仍然想使用 XML 文件,您可以使用以下脚本将 XML 转换为数据库,然后编写报告(例如 gnucashconvert filename.gnucash sqlite3:////home/username/export.sqlite ):
As Chop Suey said, GnuCash 2.4 has its own database format. If you still want to use the XML files, you can use the following script to convert from XML to a database, and then write your reports on that (e.g. gnucashconvert filename.gnucash sqlite3:////home/username/export.sqlite):
我刚刚发布了一些 python 代码,可以读取和解释 gnucash 2.6 及更高版本中使用的 sqlite3 文件格式:
https://github .com/MatzeB/pygnucash
I just published some python code that can read and interpret the sqlite3 file format used in gnucash 2.6 and higher:
https://github.com/MatzeB/pygnucash