Files
accounts/src/anz_import.py
T
jimmyandClaude Opus 5.5 971de14356 Add cashbook web app for importing ANZ statements
Local web app and CLI that fill a NZ cashbook spreadsheet (one sheet per
month, April-March) from ANZ CSV exports and PDF statements, via a
headless LibreOffice.

- Import with preview; duplicates matched on date and amount so CSV and
  PDF versions of the same period don't double up
- Statement upload (drag and drop), view, download, rename and remove,
  with per-book import status
- Month review with GST and category editing, year summary and
  six-monthly GST returns
- Docker Compose setup; books and statements live in data/ (ignored)
- Blank cashbook template (NZ Government, CC BY 4.0); code under AGPL-3.0

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 17:10:05 +13:00

50 lines
1.7 KiB
Python

#!/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()