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

项目要开始做国际化,整理了一堆的字符串,但是交给翻译公司翻译,需要提供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 TemporaryFile
from xlwt import Workbook
import re,sys,os

dir = sys.path[0]
print dir

book = 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_workbook
import codecs

workbook = 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.minidom
from xlwt import Workbook


#新建一个workbook
book = Workbook(encoding='utf-8')
sheet = book.add_sheet('Android')

#打开xml
xmldoc = xml.dom.minidom.parse('strings.xml')
code = xmldoc.getElementsByTagName('string')
row = 0
for node in code:
    for item in node.childNodes:
        sheet.write(row, 0, node.getAttribute('name'))
        sheet.write(row, 1, item.data)
    row = row+1
#保存workbook
book.save('strings.xls')

4. android中,将excel转化成xml

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#Android国际化: 将excel中的内容转化到xml中

from xml.dom import minidom
from xlrd import open_workbook
import codecs

#打开excel
workbook = open_workbook('strings.xls')

#新建xml
doc = 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()

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值