excelToJson.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. import os.path
  4. import sys
  5. import codecs
  6. import xlrd
  7. import shutil
  8. import re
  9. from excelToLua import FloatToString,StringToString,exception,int2index
  10. excelDir="../策划脚本/".decode('utf8')
  11. jsonDir="../json/".decode('utf8')
  12. g_objName=""
  13. def haveNext(table, col, row, max_cols):
  14. for c in range(col, max_cols):
  15. objType = str(table.cell_value(row, c));
  16. if(objType.find("@") == -1 or objType.find("_") != 0):
  17. return 1
  18. return 0
  19. def table2json(table, jsonfilename):
  20. nrows = table.nrows
  21. ncols = table.ncols
  22. if nrows <= 3:
  23. return
  24. f = codecs.open(jsonfilename,"w","utf-8")
  25. f.write(u"[\n")
  26. for r in range(nrows):
  27. # if r == 2:
  28. # continue
  29. f.write(u"\t[ ")
  30. for c in range(ncols):
  31. strCellValue = u""
  32. objName = str(table.cell_value(1,c))
  33. global g_objName
  34. g_objName= objName
  35. objType = str(table.cell_value(2,c))
  36. objStr = table.cell_value(r,c)
  37. if(objName.find("@") >= 0 or objName.find("_") == 0):
  38. continue
  39. if r <= 2:
  40. strCellValue = u"\"" + objStr + u"\""
  41. else:
  42. if cmp(objType, "NUMBER") == 0 or cmp(objType, "FLOAT") == 0:
  43. strCellValue = FloatToString(objStr)
  44. if strCellValue == False:
  45. print u"错误位置\t"+str(r+1)+":"+str(table.cell_value(r,1))+"\t"+int2index(c)+":"+str(table.cell_value(0,c))
  46. exception()
  47. else:
  48. strvalue = StringToString(objStr).strip();
  49. strvalue = "".join(strvalue.split("\n"))
  50. strCellValue = u"\"" + strvalue.replace("\\", "\\\\") + u"\""
  51. if c < ncols-1:
  52. isNext = haveNext(table, c+1, 1, ncols)
  53. if isNext != 0:
  54. strCellValue += u", "
  55. f.write(strCellValue)
  56. f.write(u" ]")
  57. if r <= 1 or (r == 2 and nrows > 3) or (r > 2 and r < nrows-1):
  58. f.write(u",")
  59. f.write(u"\n")
  60. f.write(u"]")
  61. f.close()
  62. return
  63. def excelToJson(excelFileName,sheets):
  64. path = excelFileName.split('.')[0]
  65. childDir = jsonDir+path
  66. if not os.path.exists(childDir) :
  67. os.makedirs(childDir)
  68. data = xlrd.open_workbook(excelDir+excelFileName)
  69. for table in data.sheets():
  70. if table.name in sheets:
  71. print 'trans :' , excelFileName,table.name
  72. destfilename = os.path.join(jsonDir, path + "/" + table.name + ".json")
  73. try:
  74. table2json(table,destfilename)
  75. except Exception,e:
  76. print Exception,":",e
  77. exception()
  78. if __name__ == '__main__':
  79. reload(sys)
  80. sys.setdefaultencoding( "utf-8" )
  81. if os.path.isdir(jsonDir):
  82. shutil.rmtree(jsonDir)
  83. os.makedirs(jsonDir)
  84. file = open("JsonConfig.txt")
  85. r = re.compile('(\t|\n|\r)')
  86. sheetMap = dict()
  87. while True:
  88. lines = file.readlines(1000)
  89. if not lines:
  90. break
  91. for line in lines:
  92. if line.startswith('#'):
  93. continue
  94. line = r.sub('',line)
  95. if line == "":
  96. continue
  97. x = line.split(':')
  98. if len(x)<2:
  99. print 'error:',line
  100. file.close()
  101. exception()
  102. else:
  103. if x[0] not in sheetMap:
  104. sheetMap[x[0]]=[]
  105. sheetMap[x[0]].append(x[1])
  106. file.close()
  107. for (k,v) in sheetMap.items():
  108. excelToJson(k,v)