# ctype: 0 empty, 1 string, 2 number, 3 date, 4 boolean, 5 error #value名字 比如 id desc for col in range(0, excel_sheet.ncols): cell = excel_sheet.cell(1, col) col_name_list.append(str(cell.value)) assert cell.ctype == 1, "found a invalid col name in col [%d] !~" % (col)
#value的类型 比如int string boolean啊 for col in range(0, excel_sheet.ncols): cell = excel_sheet.cell(2, col) col_val_type_list.append(str(cell.value)) assert cell.ctype == 1, "found a invalid col val type in col [%d] !~" % (col)
#遍历所有有效的行 for row in range(3, excel_sheet.nrows): cell_id = excel_sheet.cell(row, 0)
assert cell_id.ctype == 2, "found a invalid id in row [%d] !~" % (row)
if cell_id.value in excel_data_dict: print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value)) print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value)) print('[警告] 配置了相同的"%d"物品, 请做检查' % (cell_id.value))
# row data list row_data_list = []
for col in range(0, excel_sheet.ncols): cell = excel_sheet.cell(row, col) k = col_name_list[col] cell_val_type = col_val_type_list[col]
# ignored the string that start with '_' if str(k).startswith('#'): continue
if cell_val_type == 'string': if cell.ctype == 0: v = '''' else: v = ''%s'' % (cell.value) elif cell_val_type == 'int': if cell.ctype == 0: v = -1 else: v = int(cell.value) elif cell_val_type == 'float': if cell.ctype == 0: v = -1 else: v = float(cell.value) elif cell_val_type == 'table': if cell.ctype == 0: v = '{}' else: v = cell.value else: v = cell.value
row_data_list.append([k, v])
excel_data_dict[cell_id.value] = row_data_list
return excel_data_dict
def excel2lua(src_excel_path, tgt_lua_path): # print('[file] %s -> %s' % (src_excel_path, tgt_lua_path)) # load excel data excel_data_src = xlrd.open_workbook(src_excel_path, encoding_override = 'utf-8') for name in excel_data_src.sheet_names(): print("Worksheet name %s " % name) data_dict = parseOneSheet(excel_data_src.sheet_by_name(name)) lua_path = tgt_lua_path + name + ".lua" lua_export_file = open(lua_path, "w") lua_export_file.write('local %s = {n' % name)
for k, v in data_dict.items(): lua_export_file.write(' [%d] = {n' % k) for row_data in v: lua_export_file.write(' {0} = {1},n'.format(row_data[0], row_data[1])) lua_export_file.write(' },n')
print("输入目录为 %s, 输出目录为%sn" % (in_path, out_path)) for root, dirs, files in os.walk(in_path): for name in files: if( ".xls" in name and ".xlsx" in name ): print(root + name) excel2lua(root + name, out_path) exit(0)