Created
July 13, 2012 18:08
-
-
Save s3n5ai/3106380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, shutil, csv | |
from xml.dom import minidom | |
from xml.etree import ElementTree as ET | |
from jinja2 import Template, Environment, PackageLoader | |
class Order(): | |
pass | |
class Shipping(): | |
pass | |
class Billing(): | |
pass | |
class Payment(): | |
pass | |
class Item(): | |
pass | |
env = Environment(loader=PackageLoader('main', 'templates')) | |
template = env.get_template('template.html') | |
fileList = os.listdir('./data') | |
def get_XML_Files(fileList): | |
for f in fileList: | |
goodList = [] | |
extension = f[-4] + f[-3] + f[-2] + f[-1] | |
if extension == '.xml': | |
goodList.append(f) | |
return goodList | |
fileList = get_XML_Files(fileList) | |
outFile = open('output.html', 'w') | |
for f in fileList: | |
orderList = [] | |
myFile = open('./data/' + f, 'rb') | |
data = myFile.read().replace('&', '&') | |
element = ET.XML(data) | |
for node in element.findall('order'): | |
order = Order() | |
billing = Billing() | |
shipping = Shipping() | |
payment = Payment() | |
items = [] | |
order.date = node[6].text | |
order.invoice = node[3].text | |
order.tax_total = "%0.2f" % float(node[10].text) | |
order.shipping_total = "%0.2f" % float(node[11].text) | |
order.discount_total = "%0.2f" % float(node[12].text) | |
order.subtotal = "%0.2f" % float(node[13].text) | |
order.grand_total = "%0.2f" % float(node[14].text) | |
order.ship_method = node[65].text | |
order.email = node[58].text | |
store = node[68].text.split('\n') | |
order.store = store[0] | |
billing.firstname = node[77][11].text | |
billing.lastname = node[77][12].text | |
billing.company = node[77][13].text | |
billing.street = node[77][14].text | |
billing.city = node[77][15].text | |
billing.region = node[77][16].text | |
billing.postcode = node[77][17].text | |
billing.country = node[77][18].text | |
billing.phone = node[77][19].text | |
shipping.firstname = node[78][11].text | |
shipping.lastname = node[78][12].text | |
shipping.company = node[78][13].text | |
shipping.street = node[78][14].text | |
shipping.city = node[78][15].text | |
shipping.region = node[78][16].text | |
shipping.postcode = node[78][17].text | |
shipping.country = node[78][18].text | |
shipping.phone = node[78][19].text | |
payment.method = node[79][13].text | |
for line in node[80]: | |
item = Item() | |
item.qty = "%0.0f" % float(line[22].text) | |
item.name = line[12].text | |
item.sku = line[11].text | |
item.price = "%0.2f" % float(line[26].text) | |
item.discount = "%0.2f" % float(line[37].text) | |
item.tax = "%0.2f" % float(line[32].text) | |
item.total_price = "%0.2f" % float(line[42].text) | |
items.append(item) | |
order.shipping = shipping | |
order.billing = billing | |
order.payment = payment | |
order.items = items | |
orderList.append(order) | |
output = template.render(orderList = orderList) | |
outFile.write(output) | |
outFile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment