createDictionary_vietnam.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. import os.path
  4. import sys
  5. import codecs
  6. import xlrd
  7. import xlwt
  8. from xlwt import Workbook
  9. import shutil
  10. import re
  11. ZONE = "vietnam"
  12. logFile = "createDictionary.txt"
  13. excelDir = ".\策划脚本"
  14. csvDir = ".\工具库文件\\client_csv_config"
  15. #csvDir = "..\csv"
  16. dictDir = ".\dict"+"\\" +ZONE
  17. uiDir = "..\GameEditors\\UIEdit\\res\\ui_edit\\xmds_ui\\"
  18. scenesDir = "..\GameEditors\\GameEditor\\data\\scenes\\"
  19. itemDir = "..\GameEditors\\GameEditor\\data\\items\\"
  20. uiPattern = "([tT]?ext=\".*?\")"
  21. scenesPattern = "(<element>showname=.*?</element>)"
  22. itemsPattern = "(<PickActionName>.*?</PickActionName>)"
  23. excelDir = unicode(excelDir,'utf8')
  24. csvDir = unicode(csvDir,'utf8')
  25. dictDir = unicode(dictDir,'utf8')
  26. translatedFile = dictDir + '\\translated.xls'
  27. unTranslateFile = dictDir + '\\unTranslated.xls'
  28. g_objName=""
  29. transDict = {}
  30. unTransDict = {}
  31. def isContainChinese(check_str):
  32. for ch in check_str.decode('utf-8'):
  33. if u'\u4e00' <= ch <= u'\u9fff':
  34. return True
  35. return False
  36. def loadTranslatedDict():
  37. data = xlrd.open_workbook(translatedFile)
  38. print "Begin loadTranslatedDict : " + translatedFile
  39. for table in data.sheets():
  40. print " sheet : " + table.name
  41. try:
  42. nrows = table.nrows
  43. ncols = table.ncols
  44. if nrows <= 3:
  45. return
  46. for r in range(nrows):
  47. if ncols<2:
  48. continue
  49. key = str(table.cell_value(r,0)).strip()
  50. val = str(table.cell_value(r,1)).strip()
  51. if len(key)>0 and len(val)>0 :
  52. transDict[key] = val
  53. except Exception,e:
  54. print Exception,":",e
  55. os.system('pause')
  56. print "loadTranslatedDict OK"
  57. return
  58. def NumberToString (aFloat):
  59. strTemp = str(aFloat)
  60. if len(strTemp) == 0:
  61. return '0';
  62. if type(aFloat) != float:
  63. print strTemp + u" - 数字类型格式错误"
  64. os.system("pause")
  65. return ""
  66. strList = strTemp.split(".")
  67. if len(strList) == 1 :
  68. return strTemp
  69. else:
  70. if strList[1] == "0" :
  71. return strList[0]
  72. else:
  73. print g_objName + " : " + strTemp + u" - 类型是否应该改为FLOAT"
  74. os.system("pause")
  75. def FloatToString (aFloat):
  76. strTemp = str(aFloat)
  77. if len(strTemp) == 0:
  78. return '0';
  79. if type(aFloat) != float:
  80. print strTemp + u" - 数字类型格式错误"
  81. os.system("pause")
  82. return ""
  83. strList = strTemp.split(".")
  84. if len(strList) == 1 :
  85. return strTemp
  86. else:
  87. if strList[1] == "0" :
  88. return strList[0]
  89. else:
  90. return strTemp
  91. def StringToString (aStr):
  92. if type(aStr) != float:
  93. return str(aStr)
  94. strTemp = str(aStr)
  95. strList = strTemp.split(".")
  96. if len(strList) == 1 :
  97. return strTemp
  98. else:
  99. if strList[1] == "0" :
  100. return strList[0]
  101. else:
  102. return strTemp
  103. def haveNext(table, col, row, max_cols):
  104. for c in range(col, max_cols):
  105. objType = str(table.cell_value(row, c));
  106. if(objType.find("@") == -1):
  107. return 1
  108. return 0
  109. def table2Dict(table):
  110. type = sys.getfilesystemencoding()
  111. nrows = table.nrows
  112. ncols = table.ncols
  113. if nrows <= 3:
  114. return
  115. for r in range(nrows):
  116. rowName = str(table.cell_value(r,0))
  117. if(rowName.find("@")>=0 ):#略过@打头的行
  118. continue
  119. for c in range(ncols):
  120. strCellValue = ''
  121. objName = str(table.cell_value(1,c))
  122. global g_objName
  123. g_objName= objName
  124. if(objName.find("@") >= 0):#@打头的列为注释,直接跳过
  125. continue
  126. if r <= 2:
  127. continue
  128. objType = str(table.cell_value(2,c))
  129. if objType=='STRING':
  130. strCellValue = str(table.cell_value(r,c)).strip()
  131. print strCellValue.decode('utf-8').encode(type)
  132. if(isContainChinese(strCellValue)):
  133. #the key not in translated dictionary
  134. if not strCellValue in transDict :
  135. unTransDict[strCellValue] = ''
  136. print "--Operate ",table.name," OK----------"
  137. return
  138. def excelToDict(excelFileName,lgFile):
  139. print 'begin trans excel:' + excelFileName
  140. lgFile.write('Excel dir:' + excelFileName)
  141. lgFile.write(u"\r\n")
  142. data = xlrd.open_workbook(excelFileName)
  143. for table in data.sheets():
  144. print "sheet : " + table.name
  145. lgFile.write('\t'+'sheetCO : ' + table.name)
  146. lgFile.write(u"\r\n")
  147. sheetName = table.name.lower()
  148. if (not sheetName.startswith('config')) and (not sheetName.startswith('sheet')) and (not sheetName.startswith('remark')) and (not sheetName.startswith('(')):
  149. if(sheetName=='blackword' or sheetName=='prefix' or sheetName=='suffix'):
  150. continue
  151. try:
  152. table2Dict(table)
  153. except Exception,e:
  154. print Exception,":",e
  155. os.system('pause')
  156. print "trans excel OK"
  157. def csvToDict(csvFileName,lgFile):
  158. print 'begin trans csv:' + csvFileName
  159. lgFile.write('csv file:' + csvFileName)
  160. lgFile.write(u"\r\n")
  161. data = open(csvFileName,"r")
  162. type = sys.getfilesystemencoding()
  163. try:
  164. reader = data.readlines()
  165. for strCellValue in reader:
  166. if(isContainChinese(strCellValue)):
  167. index = strCellValue.find(',')
  168. if index > 0:
  169. val1 = strCellValue[0:index]
  170. val2 = strCellValue[index+1:len(strCellValue)]
  171. #去除首尾空格、首尾双引号,末尾逗号
  172. key = val2.strip()
  173. key = key.rstrip(',')
  174. key = key.strip('\"')
  175. key = key.strip()
  176. print key.decode('utf-8').encode(type)
  177. #the key not in translated dictionary
  178. if not key in transDict :
  179. unTransDict[key] = ''
  180. data.close()
  181. except Exception,e:
  182. print Exception,":",e
  183. os.system('pause')
  184. print "trans csv OK"
  185. def xmlToDict(xmlFileName,destPattern,lgFile):
  186. print 'begin trans xml:' + xmlFileName
  187. lgFile.write('xml file:' + xmlFileName)
  188. lgFile.write(u"\r\n")
  189. data = open(xmlFileName,"r")
  190. type = sys.getfilesystemencoding()
  191. try:
  192. reader = data.read()
  193. dest = re.compile(destPattern)
  194. res = dest.findall(reader)
  195. for elem in res:
  196. print elem
  197. strCellValue = str(elem).strip()
  198. if len(strCellValue) > 0:
  199. if(isContainChinese(strCellValue)):
  200. print strCellValue.decode('utf-8').encode(type)
  201. #the key not in translated dictionary
  202. if not strCellValue in transDict :
  203. unTransDict[strCellValue] = ''
  204. data.close()
  205. except Exception,e:
  206. print Exception,":",e
  207. os.system('pause')
  208. print "trans xml OK"
  209. def walkXmlDir(xmlDir,destPattern,lgFile):
  210. print xmlDir
  211. if not os.path.exists(xmlDir):
  212. lgFile.write(xmlDir+' xml files dir not exist!!!')
  213. lgFile.write(u"\r\n")
  214. os.system('pause')
  215. for parent,dirnames,filenames in os.walk(xmlDir):
  216. for filename in filenames:
  217. if filename.endswith(".xml") :
  218. print 'filename : ' + filename
  219. excelFileName = os.path.join(parent, filename)
  220. xmlToDict(excelFileName,destPattern,lgFile)
  221. def extractXml(lgFile):
  222. lgFile.write('==========================Begin extract xml files==================')
  223. lgFile.write(u"\r\n")
  224. walkXmlDir(uiDir, uiPattern, lgFile)
  225. walkXmlDir(scenesDir, scenesPattern, lgFile)
  226. walkXmlDir(itemDir, itemsPattern, lgFile)
  227. if __name__ == '__main__':
  228. reload(sys)
  229. sys.setdefaultencoding( "utf-8" )
  230. #if os.path.isdir(dictDir):
  231. if not os.path.exists(dictDir):
  232. os.makedirs(dictDir)
  233. try:
  234. if os.path.exists(translatedFile):
  235. loadTranslatedDict()
  236. lgFile = codecs.open(logFile,"w","utf-8")
  237. #extract xml file
  238. extractXml(lgFile)
  239. print csvDir
  240. if not os.path.exists(csvDir):
  241. lgFile.write(csvDir+' csv files dir not exist!!!')
  242. lgFile.write(u"\r\n")
  243. os.system('pause')
  244. lgFile.write('==========================Begin extract csv files==================')
  245. lgFile.write(u"\r\n")
  246. for parent,dirnames,filenames in os.walk(csvDir):
  247. for filename in filenames:
  248. print 'filename : ' + filename
  249. excelFileName = os.path.join(parent, filename)
  250. csvToDict(excelFileName,lgFile)
  251. print excelDir
  252. lgFile.write('==========================Begin extract excel files==================')
  253. lgFile.write(u"\r\n")
  254. for parent,dirnames,filenames in os.walk(excelDir):
  255. for filename in filenames:
  256. if not filename.startswith("~$"):
  257. sufix = os.path.splitext(filename)[1][1:]
  258. # print('sufix:' + sufix);
  259. if sufix == 'xls' or sufix == 'xlsx':
  260. print 'filename : ' + filename
  261. excelFileName = os.path.join(parent, filename)
  262. excelToDict(excelFileName,lgFile)
  263. lgFile.write('==========================All OK==================')
  264. lgFile.close()
  265. book = Workbook(encoding='utf-8')
  266. sheet1 = book.add_sheet('Sheet 1')
  267. row = 0;
  268. col = 0;
  269. for item in unTransDict.keys():
  270. sheet1.write(row,col,str(item))
  271. row += 1
  272. book.save(unTranslateFile)
  273. except Exception,e:
  274. print Exception,":",e
  275. # os.system('pause')