conversion of the entire column data into xml



How to retrieve the entire column from the excel and convert it to the xml using python scripts?

Actually I had this script for retrieve the data from excel and convert it into the xml. But I have oneline script for the retrieving the entire column but I can't retrieve it throwing error for that line. Can you guys let me know how to access the entire column and put it into the xml?



import xlrd
wb = xlrd.open_workbook("my_excel_file.xlsx")
sh = wb.sheet_by_index(0)
tags = [n.replace(" ", "").lower() for n in sh.row_values(0)]

result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<myItems>\n"

for row in range(1, sh.nrows):
result += " <item>\n"
for i in range(len(tags)):
tag = tags[i].encode("utf-8")
val = str(sh.row_values(row)[i]).encode("utf-8")
result += "<%s>%s</%s>\n" % (tag, val, tag)
result += "</item>\n"

#to access the entire column
colValues = sh.col_values(column, start_row=0, end_row=max)


result += "</myItems>"

# Write the result string to a file using the standard I/O.
f = open("myfile.xml", "w")
f.write(result)
f.close()

No comments:

Post a Comment