安裝Python2及使用xlrd將Excel自動生成多語言

參考:
Mac Monterey下安裝Python2

# 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容