-
Notifications
You must be signed in to change notification settings - Fork 0
/
ynab_to_gnucash.py
68 lines (57 loc) · 2.29 KB
/
ynab_to_gnucash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import csv
from datetime import datetime
from decimal import Decimal
import gnucash
from gnucash import Session, Transaction, Split, GncNumeric
def import_ynab_to_gnucash(input_file, gnucash_file):
session = Session(gnucash_file)
book = session.book
root = book.get_root_account()
with open(input_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
date = datetime.strptime(row['Date'], '%Y-%m-%d')
payee = row['Payee']
category = row['Category Group/Category']
memo = row['Memo']
outflow = Decimal(row['Outflow'] or '0')
inflow = Decimal(row['Inflow'] or '0')
amount = inflow - outflow # Positive for inflow, negative for outflow
from_account = find_account(root, row['Account'])
to_account = find_account(root, category)
trans = Transaction(book)
trans.BeginEdit()
trans.SetCurrency(from_account.GetCommodity())
trans.SetDate(date.day, date.month, date.year)
trans.SetDescription(payee)
trans.SetNotes(memo)
split1 = Split(book)
split1.SetParent(trans)
split1.SetAccount(from_account)
split1.SetValue(GncNumeric(int(amount * 100), 100))
split1.SetAmount(GncNumeric(int(amount * 100), 100))
split2 = Split(book)
split2.SetParent(trans)
split2.SetAccount(to_account)
split2.SetValue(GncNumeric(int(-amount * 100), 100))
split2.SetAmount(GncNumeric(int(-amount * 100), 100))
trans.CommitEdit()
session.save()
session.end()
session.destroy()
print("Import completed successfully.")
def find_account(root, name):
acc = root.lookup_by_name(name)
if acc is None:
acc = gnucash.Account(root.get_book())
acc.SetName(name)
acc.SetType(gnucash.ACCT_TYPE_EXPENSE)
acc.SetCommodity(root.get_commodity())
root.append_child(acc)
return acc
def main():
input_file = 'path/to/your/ynab_export.csv'
gnucash_file = 'path/to/your/gnucash_file.gnucash'
import_ynab_to_gnucash(input_file, gnucash_file)
if __name__ == "__main__":
main()