我想创建一个由电子邮件过滤器触发的脚本,该过滤器在运行时在GnuCash中创建两个事务我看到了GnuCash has Python bindings但是文档最多是稀疏的…更好的说法是“不存在”。
是否可以编写一个脚本来在GnuCash中创建事务如果是,那么最基本的代码是什么样子的?我只需要一个起点;一旦我有了,我可以自己写得相当好。

最佳答案

下面是一个使用piecash(https://github.com/sdementen/piecash,0.10.1版)的替代解决方案,这是一个使用gnucash图书的现代python库(免责声明:我是作者)

from piecash import open_book, Transaction, Split
from datetime import datetime
from decimal import Decimal

# open the book
with open_book("/path/to/file.gnucash",
               open_if_lock=True,
               readonly=False) as mybook:
    today = datetime.now()
    # retrieve the currency from the book
    USD = mybook.currencies(mnemonic="USD")
    # define the amount as Decimal
    amount = Decimal("25.35")
    # retrieve accounts
    to_account = mybook.accounts(fullname="Expenses:Some Expense Account")
    from_account = mybook.accounts(fullname="Assets:Current Assets:Checking")
    # create the transaction with its two splits
    Transaction(
        post_date=today,
        enter_date=today,
        currency=USD,
        description="Transaction Description!",
        splits=[
            Split(account=to_account,
                  value=amount,
                  memo="Split Memo!"),
            Split(account=from_account,
                  value=-amount,
                  memo="Other Split Memo!"),
        ]
    )
    # save the book
    mybook.save()

10-06 02:36