You may want to try the ubuntu package python3-ledger, since hopefully
you are using python3. Otherwise please let us know the versions of
ledger, python, and python-ledger. python-ledger is gone from current
debian, so I cannot test, but your script runs ok with python3-ledger
3.2.1-7+b2 on Debian.