Skip to main content

ledger_importer is a csv-to-ledger importer that can be configured in Python.

Project description

ledger_importer CircleCI

ledger_importer is a csv-to-ledger importer that can be configured in Python.

The key features are:

  • Customization: Designed to fit your specific needs perfectly.
  • Auto-completion: The confirmation step is auto-completed.
  • Integration: Easy to integrate with your pipeline.

ledger_importer main selling point is that if you know Python, you can write complex rules to match & parse accounts / target_accounts. All other tools try to be smart about the target_account matching part but offer very little customization (regex matching is the best I've seen).

Another cool feature is that if you have several bank accounts, you can concatenate their csv exports and ledger_importer will de-duplicate transactions between them. The de-duplication rule can be customized to your needs.

Installation

$ pip install ledger-importer

Configure

ledger_importer works by using the configuration file as the entrypoint. The ledger_importer.runner function is the function that should be called when you want to run the program.

The runner function takes a Config as the first argument.

A Config instance can be created by creating a new class that inherits ledger_importer.runner. This new class must implement the following methods:

  • parse_date(self, fields: tuple) -> datetime.datetime
  • parse_payee(self, fields: tuple) -> str
  • parse_postings(self, fields: tuple) -> list[Posting]

The argument fields: tuple will be the csv row, with each column as an element of the tuple.

Example configuration file:

#!/usr/bin/env python
from __future__ import annotations

import datetime
import re
from decimal import Decimal

from ledger_importer import Config, runner, Posting, Amount

# Custom ledger importer configuration
class LedgerImporterConfig(Config):
    # Define the number of lines that needs to be skipped at the beginning of the file.
    # This is usefull if the csv has a line with the column names for example.
    skip_lines: int = 1

    # Define the csv delimiter
    csv_delimiter: str = ";"

    # The argument `fields` given in all parse_* methods contains a whole csv row in a tuple
    # Each element of the tuple is a string representation of the column

    def parse_date(self, fields: tuple) -> datetime.datetime:
        return datetime.datetime.strptime(fields[0], "%m-%d-%Y")

    def parse_payee(self, fields: tuple) -> str:
        return fields[2]

    def parse_postings(self, fields: tuple) -> list[Posting]:
        amount = Amount(quantity=Decimal(re.sub("[€ ]", "", fields[3]).replace(",", ".")), commodity="€")
        if amount > 0:
            account = "Income"
        else:
            account = "Expenses"

        posting = Posting(account="Assets:Checking", amount=amount)

        # A Transaction should be at least 2 postings that have reversed amounts
        return [posting, Posting(account=account, amount=amount.reverse())]


# The next lines are required to run ledger_importer
# when the config file is executed.
if __name__ == "__main__":
    runner(LedgerImporterConfig())

Run

To run leger_importer, run the configuration module:

$ python my_importer.py bank-statement.csv --journal-path journal.ledger

|        Account         |    Date    |  Amount  |        Payee        |
| Assets:Account:Florent | 2021/07/29 | 1234.56€ | VIR LOLE FOOB A.R.L |

Which account provided this income? ([Income:Salary]/[q]uit/[s]kip)


|        Account         |    Date    |  Amount |            Payee             |
| Assets:Account:Florent | 2021/08/02 | -11.77€ | CARD  27/07/21 SWILE XX*XXXX |

To which account did this money go? ([Expenses:Restaurant]/[q]uit/[s]kip)


|        Account         |    Date    |   Amount  |                  Payee                  |
| Assets:Account:Florent | 2021/08/03 |  -784.00€ | VIR Save some  Mr.      Florent        |

To which account did this money go? ([Expenses]/[q]uit/[s]kip)
Assets:Savings


|        Account         |    Date    |  Amount |             Payee             |
| Assets:Account:Florent | 2021/08/03 | -58.63€ | CARD  08/03/21 PAYPAL XX*XXXX |

To which account did this money go? ([Expenses:Shopping]/[q]uit/[s]kip)
q

Usage

A sample configuration can be generated:

python -m ledger_importer > my_importer.py

The configuration can be run directly:

$ python my_importer.py --help
Usage: my_config.py [OPTIONS] CSV_PATH

  Import a bank statement.

Arguments:
  CSV_PATH  Path to the bank statement to import.  [required]

Options:
  --journal-path PATH             Path a ledger journal to write & learn
                                  accounts from.
  --quiet / --no-quiet            Don't ask questions and guess all the
                                  accounts automatically.  [default: no-quiet]
  --help                          Show this message and exit.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ledger_importer-0.4.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

ledger_importer-0.4.2-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file ledger_importer-0.4.2.tar.gz.

File metadata

  • Download URL: ledger_importer-0.4.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.13.0-40-generic

File hashes

Hashes for ledger_importer-0.4.2.tar.gz
Algorithm Hash digest
SHA256 59977a0b97473e1e83ae0445b75cc45d4bda5a63385e1b2e1cf878bf667dba09
MD5 777f0530b98fa9a32d1400f53d5ca23e
BLAKE2b-256 c19c1eb2827a09413a7e293b320373ed763f9e3aa7bc23fa7ec6f3fa903789e7

See more details on using hashes here.

File details

Details for the file ledger_importer-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: ledger_importer-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.11 CPython/3.8.10 Linux/5.13.0-40-generic

File hashes

Hashes for ledger_importer-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b1a16f827e773afd3d366ae1ff7261b46de4e1fe7a2a6acbc32a9242f8f845c0
MD5 76252c72ea58f7e3ec9bd6b09bec930e
BLAKE2b-256 4a941c1ad58d79a83538918faf153b8c1bbac54c1913f31ae1a536611c23eb9a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page