导表工具优化
parent
9053926e02
commit
608c296452
|
@ -1,3 +1,4 @@
|
||||||
|
from xlutils import copy
|
||||||
import xlwt
|
import xlwt
|
||||||
import xlrd
|
import xlrd
|
||||||
import os
|
import os
|
||||||
|
@ -11,50 +12,52 @@ sys.setdefaultencoding('utf8')
|
||||||
# test
|
# test
|
||||||
class Test():
|
class Test():
|
||||||
|
|
||||||
|
# excel line config
|
||||||
_ExportConfigXlsx = os.getcwd() + '/ExportConfig.xlsx'
|
_ExportConfigXlsx = os.getcwd() + '/ExportConfig.xlsx'
|
||||||
_ExportConfig = {}
|
_ExportConfig = {}
|
||||||
|
|
||||||
|
# orginal data
|
||||||
|
excelFilePath = os.getcwd() + '/../base_data/'
|
||||||
_col_offset = 6
|
_col_offset = 6
|
||||||
|
|
||||||
|
# translate data
|
||||||
|
_TranslateData = {}
|
||||||
|
_TranslatePath = os.getcwd() + '/../Language_data/'
|
||||||
|
_TranslateName = "LanguageXlsx"
|
||||||
|
_TranslateTitle = ['original', 'change', 'tchinese', 'english', 'korean']
|
||||||
|
|
||||||
translateXlsx = []
|
|
||||||
excelFilePath = os.getcwd() + '/../base_data/'
|
|
||||||
|
|
||||||
_ExportData = {}
|
def init(self):
|
||||||
_ExportPath = os.getcwd() + '/Export/'
|
print("init")
|
||||||
_ExportName = "LanguageXlsx"
|
|
||||||
|
|
||||||
def Show(self):
|
|
||||||
# print(self.excelFilePath)
|
|
||||||
# self.FindTranslateXlsx(self.excelFilePath)
|
|
||||||
self.LoadExportConfig()
|
self.LoadExportConfig()
|
||||||
self.Export()
|
self.LoadTranslateData()
|
||||||
|
print("init done...")
|
||||||
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")
|
|
||||||
|
|
||||||
|
|
||||||
|
####TOOL
|
||||||
|
#
|
||||||
|
def PathDataToStr(self, pathData):
|
||||||
|
pathStr = []
|
||||||
|
for _key in pathData:
|
||||||
|
path = _key
|
||||||
|
for num in pathData[_key]:
|
||||||
|
path += "|" + str(num)
|
||||||
|
pathStr.append(path)
|
||||||
|
return pathStr
|
||||||
|
#
|
||||||
|
def PathStrToData(self, pathStr):
|
||||||
|
data = {}
|
||||||
|
for path in pathStr:
|
||||||
|
_l = path.split('|')
|
||||||
|
_key = _l[0]+ "|" + _l[1]
|
||||||
|
data[_key] = []
|
||||||
|
for i in range(len(_l)):
|
||||||
|
if i > 1 and _l[i] != "":
|
||||||
|
data[_key].append(int(_l[i]))
|
||||||
|
return data
|
||||||
|
|
||||||
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
|
# English line to num line
|
||||||
def ColToNum(self, colStr):
|
def ColToNum(self, colStr):
|
||||||
i = 0
|
i = 0
|
||||||
l = len(colStr)
|
l = len(colStr)
|
||||||
|
@ -67,64 +70,153 @@ class Test():
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# def FindTranslateXlsx(self, _file_dir):
|
def LoadExportConfig(self):
|
||||||
# _fileList = os.listdir(_file_dir)
|
print("LoadExportConfig...")
|
||||||
# for _files in _fileList:
|
_workBook = xlrd.open_workbook(self._ExportConfigXlsx, 'rb')
|
||||||
# _filePath = os.path.join(_file_dir, _files)
|
if _workBook:
|
||||||
# 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)
|
_workSheet = _workBook.sheet_by_index(0)
|
||||||
_cData = _workSheet.col_values(_colNum)
|
for _rIndex in range(_workSheet.nrows):
|
||||||
for _index in range(len(_cData)):
|
if _rIndex != 0:
|
||||||
if _index >= self._col_offset:
|
_rData = _workSheet.row_values(_rIndex)
|
||||||
_key = str(_cData[_index])
|
self._ExportConfig[_rIndex - 1] = _rData
|
||||||
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):
|
def LoadTranslateData(self):
|
||||||
print("Writing...")
|
print("LoadTranslateData...")
|
||||||
|
self._TranslateData = {}
|
||||||
|
_workBook = xlrd.open_workbook(self._TranslatePath + self._TranslateName + ".xls", 'rb')
|
||||||
|
if _workBook:
|
||||||
|
_workSheet = _workBook.sheet_by_index(0)
|
||||||
|
for _rIndex in range(_workSheet.nrows):
|
||||||
|
# except title
|
||||||
|
if _rIndex != 0:
|
||||||
|
_rData = _workSheet.row_values(_rIndex)
|
||||||
|
_key = _rData[0]
|
||||||
|
# check repeat
|
||||||
|
if not self._TranslateData.has_key(_key):
|
||||||
|
self._TranslateData[_key] = self.CreateEmptyTranslateItem()
|
||||||
|
_pathStr = []
|
||||||
|
for i in range(len(_rData)):
|
||||||
|
if i < len(self._TranslateTitle):
|
||||||
|
# translate data
|
||||||
|
self._TranslateData[_key][self._TranslateTitle[i]] = _rData[i]
|
||||||
|
else:
|
||||||
|
# path data
|
||||||
|
if _rData[i].strip() != "": _pathStr.append(_rData[i])
|
||||||
|
self._TranslateData[_key]["pathData"] = self.PathStrToData(_pathStr)
|
||||||
|
|
||||||
_savePath = self._ExportPath + self._ExportName +'.xls'
|
def CreateEmptyTranslateItem(self):
|
||||||
|
o = {}
|
||||||
|
for k in self._TranslateTitle:
|
||||||
|
o[k] = ""
|
||||||
|
o["pathData"] = {}
|
||||||
|
return o
|
||||||
|
|
||||||
|
|
||||||
|
# clear all path
|
||||||
|
def ClearTranslateDataPath(self):
|
||||||
|
for ol in self._TranslateData:
|
||||||
|
self._TranslateData[ol]["pathData"] = {}
|
||||||
|
|
||||||
|
# Add a new data
|
||||||
|
def AddTranslateData(self, content, fileName, colName, rowNum):
|
||||||
|
|
||||||
|
if not self._TranslateData.has_key(content):
|
||||||
|
self._TranslateData[content] = self.CreateEmptyTranslateItem()
|
||||||
|
self._TranslateData[content]["original"] = content
|
||||||
|
|
||||||
|
_pathData = self._TranslateData[content]["pathData"]
|
||||||
|
|
||||||
|
_key = fileName + "|" + colName
|
||||||
|
if not _pathData.has_key(_key):
|
||||||
|
_pathData[_key] = []
|
||||||
|
|
||||||
|
if not _pathData[_key].__contains__(rowNum):
|
||||||
|
_pathData[_key].append(rowNum)
|
||||||
|
self._TranslateData[content]["pathData"] = _pathData
|
||||||
|
|
||||||
|
# save to local
|
||||||
|
def WriteTranslateDataToLocal(self):
|
||||||
|
print("WriteTranslateDataToLocal...")
|
||||||
|
_savePath = self._TranslatePath + self._TranslateName +'.xls'
|
||||||
_workBook = xlwt.Workbook(encoding='utf-8')
|
_workBook = xlwt.Workbook(encoding='utf-8')
|
||||||
_workSheet = _workBook.add_sheet(self._ExportName)
|
_workSheet = _workBook.add_sheet(self._TranslateName)
|
||||||
|
|
||||||
_title = ['original', 'tchinese', 'english', 'korean']
|
# title
|
||||||
_titleIndex = 0
|
_titleIndex = 0
|
||||||
for _content in _title:
|
for _content in self._TranslateTitle:
|
||||||
_workSheet.write(0, _titleIndex, _content)
|
_workSheet.write(0, _titleIndex, _content)
|
||||||
_titleIndex += 1
|
_titleIndex += 1
|
||||||
|
|
||||||
_cIndex = 1
|
|
||||||
for ol in self._ExportData:
|
|
||||||
# content
|
# content
|
||||||
_workSheet.write(_cIndex, 0, str(ol))
|
_rIndex = 1
|
||||||
|
for ol in self._TranslateData:
|
||||||
|
# translate
|
||||||
|
_cIndex = 0
|
||||||
|
for _content in self._TranslateTitle:
|
||||||
|
_workSheet.write(_rIndex, _cIndex, str(self._TranslateData[ol][_content]))
|
||||||
|
_cIndex += 1
|
||||||
|
|
||||||
# path
|
# path
|
||||||
_pIndex = 1
|
for p in self.PathDataToStr(self._TranslateData[ol]["pathData"]):
|
||||||
for p in self._ExportData[ol]:
|
_workSheet.write(_rIndex, _cIndex, str(p))
|
||||||
nums = ""
|
|
||||||
for n in self._ExportData[ol][p]:
|
|
||||||
nums += (str(n) + "#")
|
|
||||||
_workSheet.write(_cIndex, 4 + _pIndex, str(p) + "|" + nums)
|
|
||||||
_pIndex += 1
|
|
||||||
|
|
||||||
_cIndex += 1
|
_cIndex += 1
|
||||||
|
|
||||||
|
_rIndex += 1
|
||||||
|
|
||||||
_workBook.save(_savePath)
|
_workBook.save(_savePath)
|
||||||
|
|
||||||
print("Write Done")
|
print("Write Done")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def WriteXlsxDataByCol(self, content, path, fileName, colName, rowNums):
|
||||||
|
_savePath = path + fileName +'.xlsx'
|
||||||
|
_rworkBook = xlrd.open_workbook(_savePath)
|
||||||
|
_wworkBook = copy.copy(_rworkBook)
|
||||||
|
_workSheet = _wworkBook.get_sheet(0)
|
||||||
|
|
||||||
|
_colNum = self.ColToNum(colName)
|
||||||
|
for row in rowNums:
|
||||||
|
_workSheet.write(row, _colNum, content)
|
||||||
|
|
||||||
|
_wworkBook.save(_savePath)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def ExportRead(self, path, fileName, colName):
|
||||||
|
_colNum = self.ColToNum(colName)
|
||||||
|
_filePath = path + fileName + ".xlsx"
|
||||||
|
_workBook = xlrd.open_workbook(_filePath, 'rb')
|
||||||
|
_workSheet = _workBook.sheet_by_index(0)
|
||||||
|
_cData = _workSheet.col_values(_colNum)
|
||||||
|
for _index in range(len(_cData)):
|
||||||
|
if _index >= self._col_offset:
|
||||||
|
_content = str(_cData[_index])
|
||||||
|
if _content and _content != "":
|
||||||
|
self.AddTranslateData(_content, fileName, colName, _index)
|
||||||
|
|
||||||
|
def Export(self):
|
||||||
|
for _index in range(len(self._ExportConfig)):
|
||||||
|
_row = self._ExportConfig[_index]
|
||||||
|
print("Read:"+_row[0])
|
||||||
|
for i in range(len(_row)):
|
||||||
|
if i != 0 and _row[i] != "":
|
||||||
|
self.ExportRead(self.excelFilePath, _row[0], _row[i])
|
||||||
|
self.WriteTranslateDataToLocal()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def applyChange(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
t = Test()
|
t = Test()
|
||||||
t.Show()
|
t.init()
|
||||||
|
t.Export()
|
||||||
|
|
Loading…
Reference in New Issue