#!/usr/bin/env python#-*- coding: UTF-8 -*-
importsysfrom openpyxl.reader.excel importload_workbookimportosimportos.pathdefgen_data(filename):
wb= load_workbook(‘dataxlsx/‘ + filename + ‘.xlsx‘) #加载文件所有分页
sheetnames= wb.get_sheet_names() #获取所有分页的名字列表
ws = wb.get_sheet_by_name(sheetnames[0]) #取第一个分页的数据#print ‘ws:‘, ws
#print "Work Sheet Titile:", ws.title # 分页名称#print "Work Sheet Rows:", ws.max_row # 分页行数#print "Work Sheet Cols:", ws.max_column # 分页列数
content= [] #数据内容
id_list = [] #ID列表
#===============================================start concat need data================================================
content.append(‘%% this file is auto maked!\n‘)
content.append(‘-module(‘ + filename + ‘).\n‘)
content.append(‘-compile(export_all).\n‘)for i in range(4, ws.max_row + 1): #从表格第三行开始读取,由于range函数不包含文件尾,所以为了读到最后一行需+1
for j inrange(ws.max_column):if ws[i][j].value ==None:
content.append(‘,""‘)elif j ==0:
id_list.append(int(ws[i][j].value))
content.append(‘get(‘ + str(ws[i][j].value).strip() + ‘) ->\n‘)
content.append(‘{r_‘ + filename + ‘,‘ +str(ws[i][j].value).strip())else:
content.append(‘,‘ +str(ws[i][j].value).strip())
content.append(‘};\n‘)
content.append(‘get(_) ->\n‘)
content.append(‘not_match.\n‘)
content.append(‘length() ->\n‘)
content.append(‘ ‘ + str(ws.max_row - 1) + ‘.\n‘)
content.append(‘id_list() ->\n‘ + str(id_list) + ‘.‘)#=====================================================end=============================================================
#写入数据
f = file(‘./server/‘ + filename + ‘.erl‘,‘w+‘)
f.writelines(content)print ‘create new file:‘, filename + ‘.erl‘f.close()#关闭通道
return
defstart_gen():#删除旧的数据
delnames = os.listdir(‘./server‘)for delname indelnames:
os.remove(‘./server/‘ +delname)print ‘delete old file:‘, delnamefor _, _, filenames in os.walk(‘./dataxlsx‘): #遍历文件夹
for filename in filenames: #遍历文件
find = filename.find(‘.xlsx‘) #返回该文件名称长度#print "find is:", find
if filename[0] == ‘~‘ or find == -1: #文件名以‘~‘开头或者找不到文件名, 如以‘.‘开头的文件
continue
else:
split_list= filename.split(‘.‘) #使用‘.‘分割文件名,获得[文件名,文件格式]#print split_list
gen_data(split_list[0]) #用文件名作为参数调用gen_data
start_gen()