createDictionary_taiwan.py 10 KB

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