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
- python脚本11——.strings文件与excel互转、 xml文件与excel互转
- 一个python 脚本将XML文件转换到excel
- Python读写与追加excel文件
- python操作excel文件——Python Excel Tutorial 指南
- 多国语言strings.xml与excel表格互相转换
- EXCEL文件上传与下载
- \t\t将 Excel 与 xml 互转
- 文件的导入——上传与读取excel
- 下载excel、xml文件
- Python——Excel文件读写
- python读取excel文件—xlrd
- 导出xml,excel文件,导入excel文件
- iOS 国际化EXCEL转Strings文件
- python 修改Excel文件
- Python修改Excel文件
- python读写Excel文件
- python读写excel文件
- Python读写excel文件
- [翻译]mongodb日志分析工具mtools之mplotqueries
- 浅谈iClient for javascript 要素图层
- PipedOutputStream 和 PipedInputStream
- iOS提交遇到的新问题
- git 学习总结
- python脚本11——.strings文件与excel互转、 xml文件与excel互转
- php数组取数组名包含***的
- JSP EL表达式
- javascript 修改meta
- c# - Redis with RedisBoost
- 多步骤jquey可视化组件
- 日期内外格式转换函数
- JSONObject与JSONArray的使用
- Unity3d LineRenderer画线