import xml.dom.minidom
from xlrd import open_workbook
from xlwt import Workbook
from xml.dom import minidom
import codecs
# 打开xml
xmldoc = xml.dom.minidom.parse('stringen.xml')
code = xmldoc.getElementsByTagName('string')
# 打开excel
workbook = open_workbook('stringsen1.xls')
# 新建xml
doc = minidom.Document()
resources = doc.createElement('resources')
doc.appendChild(resources)
row = 1
for node in code:
for item in node.childNodes:
# xml 的id
#print("开始查找相同的id=="+node.getAttribute('name')+" value===="+ item.nodeValue)
text_element = doc.createElement('string')
text_element.setAttribute('name', node.getAttribute('name'))
for sheet in workbook.sheets():
for col in range(1,2):
# 添加根元素
temp = "null"
for row_index in range(1,sheet.nrows):
result_placeholder = sheet.cell(row_index, 0).value
result_content = sheet.cell(row_index, col).value
# 新建一个文本元素
if node.getAttribute('name')==result_placeholder :
print("key相同"+node.getAttribute('name')+ "****"+result_placeholder)
#text_element.appendChild(doc.createTextNode(result_content))
#print("key相同"+item.nodeValue)
#resources.appendChild(text_element)
temp = result_content
break
#else :
#text_element.appendChild(doc.createTextNode(item.nodeValue))
#print(item.nodeValue)
#resources.appendChild(text_element)
#print("key==="+result_placeholder+" ***valus===" + result_content)
if temp == "null" :
#print("key相同"+item.nodeValue)
text_element.appendChild(doc.createTextNode(item.nodeValue))
else:
#print("key不相同"+item.nodeValue)
text_element.appendChild(doc.createTextNode(temp))
#result_content = sheet.cell(0, col).value
resources.appendChild(text_element)
f = codecs.open("test"+'_new_strings.xml', 'w', encoding='utf-8')
f.write(doc.toprettyxml(indent=' '))
doc.removeChild(resources)
xml 跟xls 相同id把对应的翻译从 xls 复制到xml 上。
stringen.xml :android string 资源文件
stringsen1.xls xls 更新的一些翻译(客户更新的)
test_new_strings.xml 生成新的string xml 翻译