-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexcel2XMLDataMapper.py
More file actions
110 lines (86 loc) · 3.3 KB
/
Copy pathexcel2XMLDataMapper.py
File metadata and controls
110 lines (86 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# http://xlrd.readthedocs.io/en/latest/api.html#xlrd-sheet
from xlrd import open_workbook
def toUnicode(string):
return string.encode('utf-8').strip()
def escapeAmpersandCharacter(string):
return string.replace('&','&')
def escapeBracketCharacter(string):
string = string.replace('(','')
string = string.replace(')', '')
return string
def escapeApostropheCharacter(string):
return string.replace('\'','_')
def escapeForwardSlashCharacter(string):
return string.replace('/','_')
def processString(string):
string = escapeAmpersandCharacter(string)
string = toUnicode(string)
return string
def generateXMLTagName(string):
string = toUnicode(string)
string = escapeBracketCharacter(string)
string = escapeApostropheCharacter(string)
string = escapeForwardSlashCharacter(string)
return (string.lower()).replace(' ','_')
class XMLObject(object):
def __init__(self, header, values = None):
self.xml_object = '<result>\n'
self.values = values
for col in range(0,len(header)):
if (header[col] != 'NoHeader'):
self.xml_object = ''.join([self.xml_object,
'\t\t<',
generateXMLTagName(header[col]),
'>',
processString(self.values[col]),
'</',
generateXMLTagName(header[col]),
'>\n'])
self.xml_object = self.xml_object + '</result>\n'
def __str__(self):
return self.xml_object
# Open Excel Spreadsheet
wb = open_workbook('data/gravitate/Full_3D_patch_id_museumurl_plypath.xlsx')
# By adding the name of a specific sheet in the workbook as an item of the list below,
# the script will not read its content and move to the next sheet like in the example
wb_sheet_list_to_ignore = ['Sheet2','Sheet3'];
#wb_sheet_list_to_ignore = []
items = []
for sheet in wb.sheets():
if sheet.name not in wb_sheet_list_to_ignore:
number_of_rows = sheet.nrows
number_of_columns = sheet.ncols
rows = []
# Set header of the sheet -- assuming it is the first row when set to 0
header_row = 0
header = []
for col in range(number_of_columns):
header_item = (sheet.cell(header_row,col).value)
if not header_item:
header_item = 'NoHeader'
header.append(header_item)
# Read rows starting from header_row+offset
offset = 1
for row in range(header_row+offset, number_of_rows):
values = []
for col in range(number_of_columns):
value = (sheet.cell(row,col).value)
try:
value = str(int(value))
except ValueError:
pass
finally:
values.append(value)
item = XMLObject(header,values)
items.append(item)
# Creating an empty output file
xml = open('data/output.xml','w')
xml.close()
# Writing into the .xml file
xml = open('data/output.xml','a')
xml.write('<?xml version="1.0" encoding="UTF-8"?>\n')
xml.write('<results>\n')
for item in items:
xml.write(str(item))
xml.write('</results>\n')
xml.close()