-
Notifications
You must be signed in to change notification settings - Fork 6
/
convert.py
74 lines (51 loc) · 2.37 KB
/
convert.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
69
70
71
72
73
74
# Script to convert CSV to IIF output.
import os
import sys, traceback, re
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
def error(trans):
sys.stderr.write("%s\n" % trans)
traceback.print_exc(None, sys.stderr)
def main(input_file_name):
input_file = open(os.path.join(PROJECT_ROOT, input_file_name), 'r')
output_file = open(os.path.join(PROJECT_ROOT, input_file_name + '.iif'), 'w')
# This is the name of the QuickBooks checking account
account = "BofA Credit Card"
# This is the IIF template
head = "!TRNS TRNSID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO CLEAR TOPRINT NAMEISTAXABLE DUEDATE TERMS PAYMETH SHIPVIA SHIPDATE REP FOB PONUM INVMEMO ADDR1 ADDR2 ADDR3 ADDR4 ADDR5 SADDR1 SADDR2 SADDR3 SADDR4 SADDR5 TOSEND ISAJE OTHER1 ACCTTYPE ACCTSPECIAL\r\n"\
+ "!SPL SPLID TRNSTYPE DATE ACCNT NAME CLASS AMOUNT DOCNUM MEMO CLEAR QNTY PRICE INVITEM PAYMETH TAXABLE EXTRA VATCODE VATRATE VATAMOUNT VALADJ SERVICEDATE TAXCODE TAXRATE TAXAMOUNT TAXITEM OTHER2 OTHER3 REIMBEXP ACCTTYPE ACCTSPECIAL ITEMTYPE\r\n"\
+ "!ENDTRNS\r\n"
output_file.write(head)
template = "TRNS CREDIT CARD %s BofA Credit Card -%s %s N N %s N CCARD\r\n"\
+ "SPL CREDIT CARD %s Ask My Accountant %s 0 %s 0.00 0.00 EXP\r\n"\
+ "ENDTRNS\r\n"
# And here's the part that inserts data into the tempalate
for trans in input_file:
trans = trans.strip()
if trans == "":
continue
try:
list = trans.split(',')
assert (len(list) == 3 )
except:
error(trans)
continue
try:
(date, amount, comments) = list
# date = date.replace('/', '-')
except:
error(trans)
continue
try:
amount = float(amount)
except:
error(trans)
continue
comments = comments.strip('"')
comments = comments.strip("\n")
comments = comments.strip("\r")
output_file.write(template % (date, amount, comments, date,
date, amount, amount))
if __name__ == '__main__':
if len(sys.argv) != 2:
print "usage: python convert.py input.csv"
main(sys.argv[1])