#coding=utf-8
import xlwt
import xlrd
import codecs
import sys
from xml.dom import minidom
from HTMLParser import HTMLParser
import xml.etree.ElementTree as ET
def toXML(filePath):
data = xlrd.open_workbook(filePath);
global sheet
sheet = data.sheet_by_name("translate")
html = HTMLParser()
rows = sheet.nrows
cols = sheet.ncols
global row
global col
row = 1
col = 3
for col in range(2,cols):
impl = minidom.getDOMImplementation()
dom = impl.createDocument(None, 'resources', None)
root = dom.documentElement
title = sheet.cell(0, col).value
print col, title
for row in range(1,rows):
attri = sheet.cell(row, 1).value
var = sheet.cell(row, col).value
print var
xmlItem = dom.createElement('string')
val = dom.createTextNode(var)
xmlItem.setAttribute("name", attri)
xmlItem.appendChild(val)
root.appendChild(xmlItem)
print dom.toprettyxml(encoding='utf-8')
s = html.unescape(dom.toprettyxml(encoding='utf-8').decode('utf-8'))
f = open('string' + title + '.xml', 'w')
f.write(s)
f.close()
def readPath():
filePath = raw_input("拖入本地化Excel \n 注意: 需要xls格式, 第一行为语言简写,第一列为场景或空,第二列为ID,第三列开始是翻译文案,sheet 名字为:translate\n")
print "文件路径是:" + filePath
return filePath
reload(sys)
sys.setdefaultencoding('utf-8')
filePath = readPath()
toXML(filePath)
xls文件格式
目前还存在转换完成的XML需要人工手动对应到values的strings.xml文件并粘贴进去。自动添加的话,需要约定规则。如有使用者,自己改进吧。
主要解决了文本中使用HTML标签,在写入到文件时自动转义特殊字符的问题。