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.

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_description(self, fields: tuple) -> str
  • parse_amount(self, fields: tuple) -> Decimal
  • format_amount(self, amount: Decimal) -> str
  • parse_payee(self, fields: tuple) -> str
  • parse_account(self, fields: tuple) -> str

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

Example configuration file:

# Name: my_importer.py

from __future__ import annotations

import datetime
import re
from decimal import Decimal

from ledger_importer import Config, runner

EXPENSES_MATCHER = {
    "Expenses:Groceries": [
        "costco",
    ],
    "Expenses:Subscriptions": [
        "netflix",
        "spotify",
    ],
    "Expenses:Shopping": [
        "amazon",
        "paypal",
    ],
}


class LedgerImporterConfig(Config):
    """
    Our custom importer configuration.

    All methods defined here must be defined to parse the csv.
    """
    skip_lines: int = 1

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

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

    def parse_amount(self, fields: tuple) -> Decimal:
        return Decimal(re.sub("[$,]", "", fields[3))

    def format_amount(self, amount: Decimal) -> str:
        return f"${amount}"

    def parse_payee(self, fields: tuple) -> str:
        if self.parse_amount(fields) > 0:
            return "Income"

        # Match the transaction with a payee based on regexp defined
        # in EXPENSES_MATCHER.
        for payee, exps in EXPENSES_MATCHER.items():
            for exp in exps:
                if re.match(rf".*{exp}.*", fields[2]):
                    return payee

        # Default to Expenses
        return "Expenses"

    def parse_account(self, fields: tuple) -> str:
        return "Assets:Checking"


# 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  |     Description     |
| 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 |         Description          |
| 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  |               Description               |
| 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 |          Description          |
| 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

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

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

Options:
  --journal-path PATH             Path a ledger journal to write & learn
                                  accounts from.
  --install-completion [bash|zsh|fish|powershell|pwsh]
                                  Install completion for the specified shell.
  --show-completion [bash|zsh|fish|powershell|pwsh]
                                  Show completion for the specified shell, to
                                  copy it or customize the installation.
  --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.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

ledger_importer-0.1.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ledger_importer-0.1.1.tar.gz
  • Upload date:
  • Size: 5.6 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.1.1.tar.gz
Algorithm Hash digest
SHA256 fbd8ed09d03ed6b5e30df6d8405197c1a41ad6af279b981b6870bea18f4d9537
MD5 2413020a4691898dbe592109c55c8293
BLAKE2b-256 f2ff2822c233156ae5a6eb8ce322711cfc2ddf48bcbbaac6a560ef21a3d48332

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ledger_importer-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e4b0032ec71bbf296390fd602e2298c8b81366a075e012a4531c3afbc1c50e98
MD5 340eeccb4084441b6b62966153ecdf34
BLAKE2b-256 b503719fe82cda43fed69538cc6342c2f647b106d174f7f7825d82164de310c8

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