131 lines
3.8 KiB
Python
131 lines
3.8 KiB
Python
import xlwt
|
|
import xlrd
|
|
import os
|
|
|
|
#
|
|
import sys
|
|
reload(sys)
|
|
sys.setdefaultencoding('utf8')
|
|
|
|
|
|
# test
|
|
class Test():
|
|
|
|
_ExportConfigXlsx = os.getcwd() + '/ExportConfig.xlsx'
|
|
_ExportConfig = {}
|
|
|
|
_col_offset = 6
|
|
|
|
|
|
translateXlsx = []
|
|
excelFilePath = os.getcwd() + '/../base_data/'
|
|
|
|
_ExportData = {}
|
|
_ExportPath = os.getcwd() + '/Export/'
|
|
_ExportName = "LanguageXlsx"
|
|
|
|
def Show(self):
|
|
# print(self.excelFilePath)
|
|
# self.FindTranslateXlsx(self.excelFilePath)
|
|
self.LoadExportConfig()
|
|
self.Export()
|
|
|
|
def LoadExportConfig(self):
|
|
_workBook = xlrd.open_workbook(self._ExportConfigXlsx, 'rb')
|
|
if _workBook:
|
|
_workSheet = _workBook.sheet_by_index(0)
|
|
for _rIndex in range(_workSheet.nrows):
|
|
if _rIndex != 0:
|
|
_rData = _workSheet.row_values(_rIndex)
|
|
self._ExportConfig[_rIndex - 1] = _rData
|
|
else:
|
|
print("Error: Unknow ExportConfig")
|
|
|
|
|
|
|
|
def Export(self):
|
|
for _index in range(len(self._ExportConfig)):
|
|
_row = self._ExportConfig[_index]
|
|
_path = self.excelFilePath + _row[0] + ".xlsx"
|
|
print("Read:"+_path)
|
|
for i in range(len(_row)):
|
|
if i != 0 and _row[i] != "":
|
|
colNum = self.ColToNum(_row[i])
|
|
self.ReadXlsxData(_path, colNum, _row[0], _row[i])
|
|
self.WriteXlsxData()
|
|
|
|
# get line num
|
|
def ColToNum(self, colStr):
|
|
i = 0
|
|
l = len(colStr)
|
|
num = 0
|
|
while i < l:
|
|
num += ((ord(colStr[l - i - 1]) - 64) * (26 ** i))
|
|
i = i + 1
|
|
return num - 1
|
|
|
|
|
|
|
|
|
|
# def FindTranslateXlsx(self, _file_dir):
|
|
# _fileList = os.listdir(_file_dir)
|
|
# for _files in _fileList:
|
|
# _filePath = os.path.join(_file_dir, _files)
|
|
# if os.path.isdir(_filePath):
|
|
# self.FindTranslateXlsx(_filePath)
|
|
# else:
|
|
# self.translateXlsx.append(_filePath)
|
|
# self.ReadXlsxData()
|
|
|
|
|
|
def ReadXlsxData(self, _path, _colNum, excelName, excelColName):
|
|
_workBook = xlrd.open_workbook(_path, 'rb')
|
|
_workSheet = _workBook.sheet_by_index(0)
|
|
_cData = _workSheet.col_values(_colNum)
|
|
for _index in range(len(_cData)):
|
|
if _index >= self._col_offset:
|
|
_key = str(_cData[_index])
|
|
if _key and _key != "":
|
|
if not self._ExportData.has_key(_key):
|
|
self._ExportData[_key] = {}
|
|
_oName = excelName + "|"+excelColName
|
|
if not self._ExportData[_key].has_key(_oName):
|
|
self._ExportData[_key][_oName] = []
|
|
self._ExportData[_key][_oName].append(_index)
|
|
|
|
|
|
def WriteXlsxData(self):
|
|
print("Writing...")
|
|
|
|
_savePath = self._ExportPath + self._ExportName +'.xls'
|
|
_workBook = xlwt.Workbook(encoding='utf-8')
|
|
_workSheet = _workBook.add_sheet(self._ExportName)
|
|
|
|
_title = ['original', 'tchinese', 'english', 'korean']
|
|
_titleIndex = 0
|
|
for _content in _title:
|
|
_workSheet.write(0, _titleIndex, _content)
|
|
_titleIndex += 1
|
|
|
|
_cIndex = 1
|
|
for ol in self._ExportData:
|
|
# content
|
|
_workSheet.write(_cIndex, 0, str(ol))
|
|
|
|
# path
|
|
_pIndex = 1
|
|
for p in self._ExportData[ol]:
|
|
nums = ""
|
|
for n in self._ExportData[ol][p]:
|
|
nums += (str(n) + "#")
|
|
_workSheet.write(_cIndex, 4 + _pIndex, str(p) + "|" + nums)
|
|
_pIndex += 1
|
|
|
|
_cIndex += 1
|
|
|
|
_workBook.save(_savePath)
|
|
|
|
print("Write Done")
|
|
t = Test()
|
|
t.Show()
|