# 1、安裝輔助環(huán)境
brew install pyenv
# 2、安裝python
pyenv install 2.7.18
# 3、設(shè)置全局默認(rèn) pyenv global 2.7.18
pyenv global 2.7.18
# 4、將python配置到配置環(huán)境里
# 在 ~/.zshrc里添加一行
export PATH="$HOME/.pyenv/versions/2.7.18/bin:$PATH"
# 讓配置生效
source ~/.zshrc
# 查看版本
python --version
參考:
https://github.com/CatchZeng/Localizable.strings2Excel
安裝支持解析xlsx
sudo pip install pyExcelerator
# sudo pip install xlrd
sudo pip install xlrd==1.2.0
xlrd.biffh.XLRDError: Excel xlsx file; not supported
參考:在升級 xlrd 2.0.1 之后不支持 xlsx 的應(yīng)對方法
sudo pip uninstall xlrd
sudo pip install xlrd==1.2.0
1、下載github:https://github.com/CatchZeng/Localizable.strings2Excel
2、創(chuàng)建python文件如下:xlsx2string.py

image.png
# -*- coding:utf-8 -*-
from XlsFileUtil import XlsFileUtil
import os
"""
解析表頭為文件夾
"""
def readName(folderPath, val):
name = val.split("(")[1]
name = name.split(")")[0]
langFolderPath = ""
if folderPath.endswith("/"):
langFolderPath = folderPath + name + ".lproj"
else:
langFolderPath = folderPath + "/" + name + ".lproj"
print "colName:%s foldName:%s path:%s" % (val, name, langFolderPath)
# 如果文件夾不存在,則生成
if not os.path.exists(langFolderPath):
os.makedirs(langFolderPath)
filePath = langFolderPath + "/Localizable.strings"
return filePath
"""
解析每一個列文多語言文件
"""
def readCols(targetDir, sheet, colIndex):
# 讀取每一行
iosFileManager = None
for rowIndex, row in enumerate(sheet.get_rows()):
if rowIndex == 0:
LocalizablePath = readName(targetDir, row[colIndex].value)
if os.path.exists(LocalizablePath):
#刪除文件,path為文件路徑
os.remove(LocalizablePath)
# 開啟讀文件
iosFileManager = open(LocalizablePath, "wb")
else:
strKey = row[1].value
strVal = row[colIndex].value
print "strKey:%s strVal:%s" % (strKey, strVal)
if(not strKey):
# 沒有key的行為注釋行
moduleName = row[0].value
content = "\n// " + moduleName + "\n"
iosFileManager.write(content)
else:
# 內(nèi)容行
content = "\"" + strKey + "\" " + "= " + "\"" + strVal + "\";\n"
iosFileManager.write(content)
# 保存關(guān)閉
iosFileManager.close()
print "end converting"
def startConvert(xlsxPath, targetDir):
print "Start converting"
if xlsxPath is None:
print "xls files directory can not be empty! try -h for help."
return
if targetDir is None:
print "Target file directory can not be empty! try -h for help."
return
# 解析xls
xlsFileUtil = XlsFileUtil(xlsxPath)
# 讀取所有tab
sheet = xlsFileUtil.getAllTables()[0]
colCount = sheet.ncols
print "colCount: %d" % (colCount)
for colIndex in range(2, colCount):
print "colIndex: %d" % (colCount)
readCols(targetDir, sheet, colIndex)
print "end converting"
def main():
# 解析參數(shù)
xlsxPath = "examples/helloi18n.xlsx"
targetDir = "examples/output/ios/i18n"
# 開始轉(zhuǎn)換
startConvert(xlsxPath, targetDir)
# python python/xlsx2string.py
main()
3、準(zhǔn)備Excel文件

image.png
4、執(zhí)行文件

image.png