#!/usr/bin/env python3 """Import ANZ (NZ) statements (CSV exports or PDF statements) into a cashbook. Each transaction is written to the month sheet (APR..MAR) matching its date, appended after any rows already filled in. Rows that are already present (same date, reference and amount) are skipped, so re-importing is safe. Usage: anz_import.py BOOK.ods STATEMENT [STATEMENT ...] [--force] Needs LibreOffice (uses its bundled Python UNO bridge); the book must not be open in LibreOffice while this runs. """ import argparse import os import shutil import sys from cashbook import YearMismatch, import_transactions from office import BookLocked, open_book from statements import read_statement def main(): ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) ap.add_argument("book", help="cashbook .ods to fill in") ap.add_argument("statements", nargs="+", help="ANZ statement(s): .csv export or .pdf") ap.add_argument("--force", action="store_true", help="import even if the transaction year doesn't match the sheet's year") args = ap.parse_args() txns = [t for path in args.statements for t in read_statement(path)] book = os.path.abspath(args.book) shutil.copy2(book, book + ".bak") try: with open_book(book) as doc: result = import_transactions(doc, txns, args.force) doc.store() except YearMismatch as e: sys.exit(f"{e}; set the start year on 'Summary report' or use --force") except BookLocked as e: sys.exit(str(e)) for name, (added, total) in result.items(): print(f"{name}: added {added} of {total} transactions") print(f"Saved {book} (backup at {book}.bak)") if __name__ == "__main__": main()