Created
September 8, 2017 12:40
-
-
Save makgithub/9aef79525222a637b9a3d811eabdb337 to your computer and use it in GitHub Desktop.
Excel to Xml file conversion using Python
This file contains 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
from openpyxl import load_workbook | |
import datetime | |
import xml.etree.cElementTree as ET | |
wb = load_workbook(filename = 'Sku Assignment 8.24 Furniture Content.xlsx') | |
sheet = wb.get_sheet_by_name('Output Sheet')#sheet name | |
root = ET.Element("skuProducts") | |
i=0 | |
for rowOfCellObjects in sheet.rows: | |
doc = ET.SubElement(root, "skuProduct"+str(i)) | |
j=0 | |
for cellObj in rowOfCellObjects: | |
print(cellObj.coordinate, cellObj.value) | |
print type(cellObj.value) | |
datatype = type(cellObj.value) | |
if( datatype is unicode): | |
ET.SubElement(doc, cellObj.coordinate).text = cellObj.value | |
elif( datatype is long): | |
print int(cellObj.value) | |
ET.SubElement(doc, cellObj.coordinate).text = str(int(cellObj.value)) | |
elif isinstance(cellObj.value, datetime.date): | |
#print cellObj.value.strftime('%m/%d/%Y') | |
#pass | |
ET.SubElement(doc, cellObj.coordinate).text = str(cellObj.value.strftime('%m/%d/%Y')) | |
else: | |
print "pass" | |
pass | |
j=j+1 | |
i=i+1 | |
print('--- END OF ROW ---') | |
tree = ET.ElementTree(root) | |
tree.write("filename.xml") |
Can you pls provide the Automation tool code?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please attach the excel file below for reference