-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExcel2StringXmlI8n.py
More file actions
executable file
·56 lines (46 loc) · 2.27 KB
/
Copy pathExcel2StringXmlI8n.py
File metadata and controls
executable file
·56 lines (46 loc) · 2.27 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.dom
import xlrd # xlrd用来读excel文件
def excel2StringXml():
data = xlrd.open_workbook("LauguageExcel.xls") # 打开excel文件
tab = data.sheet_by_index(0) # 选择excel里面的Sheet
nrows = tab.nrows # 行数
ncols = tab.ncols # 列数
for y in range(1, ncols):
# type(tab.cell(0, y).value) == str and
if tab.cell(0, y).value != '':
fileName = tab.cell(0, y).value
languageType = y
print(languageType, "out")
dom1 = xml.dom.getDOMImplementation() # 创建文档对象,文档对象用于创建各种节点。
doc = dom1.createDocument(None, "resources", None)
doc.documentElement.setAttribute('xmlns:xliff', 'urn:oasis:names:tc:xliff:document:1.2')
top_element = doc.documentElement # 得到根节点
for x in range(0, nrows):
# if x == 1000:
# break
sNode = doc.createElement('string')
if type(tab.cell(x, 0).value) == float or x == 0:
# sNode.setAttribute('name', str(int(tab.cell(x, 0).value)))
continue # 过滤Key为数字请求码
else:
sNode.setAttribute('name', tab.cell(x, 0).value)
# 给这个节点加入文本,文本也是一种节点
# if type(tab.cell(x, languageType).value) == str:
# print(languageType, "middle")
# text = doc.createTextNode(str(tab.cell(x, languageType).value))
# else:
# text = doc.createTextNode(" ")
text = doc.createTextNode(tab.cell(x, languageType).value)
# text = doc.createTextNode(content)
# print(text)
sNode.appendChild(text)
top_element.appendChild(sNode)
print(languageType, "in")
# print('strings'+ fileName +'.xml')
f = open('strings' + fileName + '.xml', 'wb+') # xml文件输出路径
f.write(doc.toprettyxml(encoding='utf-8')) # 去掉后缀.encode(encoding='utf-8') 新增toXMLencoding
f.close()
else:
break