-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcsvtoqbo.py
107 lines (91 loc) · 4.26 KB
/
csvtoqbo.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#####################################################################
# #
# File: csvtoqbo.py #
# Developer: Justin Leto #
# #
# main utility script file Python script to convert CSV files #
# of transactions exported from various platforms to QBO for #
# import into Quickbooks Online. #
# #
# Usage: python csvtoqbo.py <options> <csvfiles> #
# #
#####################################################################
import sys, traceback
import os
import logging
import csv
import qbo
import amazonpayments
# If only utility script is called
if len(sys.argv) <= 1:
sys.exit("Usage: python %s <options> <csvfiles>\n"
"Where possible options include:\n"
" -amazon Specify csv output is from Amazon Payments.\n"
" --help Help for using this tool." % sys.argv[0]
)
# If help is requested
elif (sys.argv[1] == '--help'):
sys.exit("Help for %s not yet implemented." % sys.argv[0])
# Test for valid options, instantiate appropiate provider object
if sys.argv[1] == '-amazon':
myProvider = None
myProvider = amazonpayments.amazonpayments()
# For each CSV file listed for conversion
for arg in sys.argv:
if sys.argv.index(arg) > 1:
try:
with open(arg[:len(arg)-3] + 'log'):
os.remove(arg[:len(arg)-3] + 'log')
except IOError:
pass
logging.basicConfig(filename=arg[:len(arg)-3] + 'log', level=logging.INFO)
logging.info("Opening '%s' CSV File" % myProvider.getName())
try:
with open(arg, 'r') as csvfile:
# Open CSV for reading
reader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
#instantiate the qbo object
myQbo = None
myQbo = qbo.qbo()
txnCount = 0
for row in reader:
txnCount = txnCount+1
sdata = str(row)
#read in values from row of csv file
status = myProvider.getStatus(myProvider,row)
date_posted = myProvider.getDatePosted(myProvider,row)
txn_type = myProvider.getTxnType(myProvider,row)
to_from_flag = myProvider.getToFrom(myProvider,row)
txn_amount = myProvider.getTxnAmount(myProvider,row)
name = myProvider.getTxnName(myProvider,row)
try:
#Add transaction to the qbo document
if myQbo.addTransaction(status, date_posted, txn_type, to_from_flag, txn_amount, name):
print('Transaction [' + str(txnCount) + '] added successfully!')
logging.info('Transaction [' + str(txnCount) + '] added successfully!')
except:
#Error adding transaction
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
print(''.join('!! ' + line for line in lines))
logging.info("Transaction [" + str(txnCount) + "] excluded!")
logging.info('>> Data: ' + str(sdata))
pass
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
print(''.join('!! ' + line for line in lines))
logging.info("Trouble reading CSV file!")
# After transactions have been read, write full QBO document to file
try:
filename = arg[:len(arg)-3] + 'qbo'
if myQbo.Write('./'+ filename):
print("QBO file written successfully!")
#log successful write
logging.info("QBO file %s written successfully!" % filename)
except:
#IO Error
exc_type, exc_value, exc_traceback = sys.exc_info()
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
print(''.join('!! ' + line for line in lines))
logging.info(''.join('!! ' + line for line in lines))