Skip to content

Instantly share code, notes, and snippets.

@ryan-blunden
Created July 2, 2012 06:09
Show Gist options
  • Save ryan-blunden/3031384 to your computer and use it in GitHub Desktop.
Save ryan-blunden/3031384 to your computer and use it in GitHub Desktop.
Transform a Blinksale XML document into list of invoices in CSV format
#!/usr/bin/env python
'''Transform a Blinksale XML document into list of invoices in CSV format'''
__author__ = 'Ryan Blunden'
__copyright__ = 'Copyright 2012, Ryan Blunden'
__license__ = 'GPL'
import sys
import csv
from lxml import etree
def blinksale(input_file, output_file):
root = etree.XML(open(input_file, 'r').read())
output = csv.writer(open(output_file, 'w'), dialect='excel')
output.writerow(['Client Name', 'Invoice Date', 'Invoice ID', 'Subtotal', 'Tax', 'Total'])
clients = []
for child in root.iterchildren():
if child.tag == 'clients':
clients = child
break
for client in clients.iterchildren():
client_name = client[0].text
for invoice in client[-2].iterchildren():
invoice_number = invoice[1].text
invoice_date = invoice[4].text
invoice_tax = invoice[7].items()[0][1]
invoice_total = invoice[-2].items()[0][1]
output.writerow([client_name, invoice_date, invoice_number, invoice_total, invoice_tax, invoice_total])
if __name__ == '__main__':
if len(sys.argv) < 3:
print 'Usage: %s blinksale.xml invoices.csv' % sys.argv[0]
sys.exit(1)
blinksale(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment