python脚本11——.strings文件与excel互转、 xml文件与excel互转

来源:互联网 发布:动态ppt软件 编辑:程序博客网 时间:2024/06/14 00:32

项目要开始做国际化,整理了一堆的字符串,但是交给翻译公司翻译,需要提供excel格式的,目前iOS的在无数个.strings文件中,android的则统一在.xml文件中。

此处大批量重复工作,必须要由脚本来解决。

参考了一些资料: 

python中excel的使用

http://www.simplistix.co.uk/presentations/python-excel.pdf

python中正则表达式的使用

http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html

python中xml的使用

http://www.cnblogs.com/coser/archive/2012/01/10/2318298.html

xml没有仔细看,依样画葫芦了,后来找到了比较全的资料:https://docs.python.org/2/library/xml.dom.html


1. iOS中,将.strings文件转化到excel,每一个文件单独建一个sheet

#!/usr/bin/env python# -*- coding:utf-8 -*-#iOS国际化: 将.strings文件中对应的字符串解析到excel中from tempfile import TemporaryFilefrom xlwt import Workbookimport re,sys,osdir = sys.path[0]print dirbook = Workbook(encoding='utf-8')pattern = re.compile(r'".+?"') #匹配 "xxx" = "xxx",得到两个字符串for file in os.listdir(dir): #遍历当前目录    fileName = os.path.splitext(file)[0]    fileExt = os.path.splitext(file)[1]    if os.path.isfile(file) and fileExt == '.strings':#        print 'processing file: ', file        sheet = book.add_sheet(fileName)        f = open(file, "r")        if fileName == 'InfoPlist':            row_index = 0            for line in f:                match = re.match(r'\w+=', line) #匹配 xxx="xxx",得到两个字符串                if match:                    sheet.write(row_index,0,match.group()[:-1])                match2 = pattern.search(line)                if match2:                    sheet.write(row_index,1,match2.group()[1:-1])            row_index = row_index + 1        else:            row_index = 0            for line in f:                if line.startswith('"'):                    #TODO:分割字符串,"占位符" = "内容";                    col_index = 0                    for m in pattern.finditer(line):                        sheet.write(row_index,col_index,m.group()[1:-1])                        col_index = col_index + 1                    row_index = row_index + 1                            pass        f.close()book.save('simple.xls')book.save(TemporaryFile())

2. iOS中,将excel文件转化成.strings文件,一个sheet是一个单独的.strings文件

#!/usr/bin/env python# -*- coding:utf-8 -*-#iOS国际化: 将excel中的内容转化成对应的.strings文件from xlrd import open_workbookimport codecsworkbook = open_workbook('simple.xls')for sheet in workbook.sheets():    print 'Sheet name: ', sheet.name    #按照sheet的name生成.strings文件    file = codecs.open(sheet.name + '.strings','w+',encoding='utf-8')    for row_index in range(sheet.nrows):        result_placeholder = sheet.cell(row_index,0).value        result_content = sheet.cell(row_index,1).value            if sheet.name == 'InfoPlist':            file.write(result_placeholder + '="' + result_content + '";\n')        else:            file.write('"'+result_placeholder + '" = "' + result_content + '";\n');    file.close()

3. android中,将xml转化成excel

#!/usr/bin/env python# -*- coding:utf-8 -*-#Android国际化: 将xml文件中对应的字符串解析到excel中import xml.dom.minidomfrom xlwt import Workbook#新建一个workbookbook = Workbook(encoding='utf-8')sheet = book.add_sheet('Android')#打开xmlxmldoc = xml.dom.minidom.parse('strings.xml')code = xmldoc.getElementsByTagName('string')row = 0for node in code:    for item in node.childNodes:        sheet.write(row, 0, node.getAttribute('name'))        sheet.write(row, 1, item.data)    row = row+1#保存workbookbook.save('strings.xls')

4. android中,将excel转化成xml

#!/usr/bin/env python# -*- coding:utf-8 -*-#Android国际化: 将excel中的内容转化到xml中from xml.dom import minidomfrom xlrd import open_workbookimport codecs#打开excelworkbook = open_workbook('strings.xls')#新建xmldoc = minidom.Document()#添加根元素resources = doc.createElement('resources')doc.appendChild(resources)#添加字符串for sheet in workbook.sheets():    for row_index in range(sheet.nrows):        result_placeholder = sheet.cell(row_index,0).value        result_content = sheet.cell(row_index,1).value        #新建一个文本元素        text_element = doc.createElement('string')        text_element.setAttribute('name', result_placeholder)        text_element.appendChild(doc.createTextNode(result_content))        resources.appendChild(text_element)f = codecs.open('new_strings.xml','w',encoding='utf-8')#doc.writexml(f)f.write(doc.toprettyxml(indent='    '))f.close()

注: 以上脚本都只是纯粹根据需求,能用就成。不一定适合所有人。

0 0
原创粉丝点击