Python读取xlsx翻译文案

首先安装Python,然后安装模块

//查找模块(非必须)
pip search xlrd
//安装模块
pip install xlrd

 

由于输出要是utf-8所以需要设置默认环境为utf-8

# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

 

代码如下:

# -*- coding: utf-8 -*-
#coding=utf-8
#######################################################
#filename:
#author:
#date:xxxx-xx-xx
#function:读excel文件中的数据
#######################################################
import xlrd
import math
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# True
showLog = False
dirPath = '/Users/mac/Documents/testXlsx/'
xlsxPath = 'xxxxx.xlsx'
supportLans = ['zh_CN','En','Id']
Xlfile = xlrd.open_workbook(dirPath + xlsxPath)
lanFiles = []
for supportLan in supportLans:
    lanName = dirPath + supportLan + '.txt'
    f = open(lanName,'w')
    lanFiles.append(f)
sheets = Xlfile.sheets()
for sheetPage in sheets:
    for f in lanFiles:
        f.write('/*\n\t//'+ sheetPage.name +'\n*/\n')
    if showLog:
        print ('/*\n\t//'+ sheetPage.name +'\n*/\n')
    rows = sheetPage.nrows
    columns = sheetPage.ncols
    for curr_row in range(rows):
        row = sheetPage.row_values(curr_row)
        if curr_row != 0 :
            for curr_col in range(columns):
                if row[0]:
                    if curr_col != 0:
                        f = lanFiles[curr_col - 1]
                        t = '\"' + row[curr_col] + '\";'
                        f.write(t)
                        if showLog:
                            print row[curr_col]
                    else:
                        wordIndex = row[curr_col]
                        index = int(wordIndex)
                        for f in lanFiles:
                            f.write('\"' + str(index) + '\" = ')
                        if showLog:
                            print str(index)
            for f in lanFiles:
                f.write('\n')
            if showLog:
                print '\n'
for f in lanFiles:
    f.close()
print '翻译文案读取处理结束'

 

posted @ 2019-04-18 15:24  雨筱逸悠  阅读(223)  评论(0编辑  收藏  举报