Skip to content

Instantly share code, notes, and snippets.

@uncycler
Created January 21, 2019 13:07
Show Gist options
  • Save uncycler/b18e3a9baaaa00deb995cef50087f19d to your computer and use it in GitHub Desktop.
Save uncycler/b18e3a9baaaa00deb995cef50087f19d to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import xml.dom.minidom
import csv
import glob
for file in glob.glob('CAMT.053*.xml'):
print(file)
camt = xml.dom.minidom.parse(file)
document = camt.documentElement
csvfile = open(file.replace('.xml','.csv'), 'w', newline='')
csvwriter = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(['Date','Description','Amount'])
balance_before = float(document.getElementsByTagName('Bal')[0].getElementsByTagName('Amt')[0].firstChild.data)
balance_after = float(document.getElementsByTagName('Bal')[1].getElementsByTagName('Amt')[0].firstChild.data)
balance = 0
for entry in document.getElementsByTagName('Ntry'):
name = entry.getElementsByTagName('AddtlNtryInf')[0].firstChild.data
date = entry.getElementsByTagName('BookgDt')[0].getElementsByTagName('Dt')[0].firstChild.data
for entry_detail in entry.getElementsByTagName('NtryDtls'):
for transaction in entry_detail.getElementsByTagName('TxDtls'):
amount = transaction.getElementsByTagName('Amt')[0].firstChild.data
direction = transaction.getElementsByTagName('CdtDbtInd')[0].firstChild.data
if(direction == 'DBIT'): amount = float(amount)*-1
if(transaction.getElementsByTagName('Nm')):
name = transaction.getElementsByTagName('Nm')[0].firstChild.data
print(date + ' : ' + name + ' : ' + str(amount))
csvwriter.writerow([date,name,str(amount)])
balance = balance + float(amount)
print(str(balance))
print(str(balance_before) + ' - ' + str(balance_after) + ' = ' + str(balance_after - balance_before))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment