import xlrd, xlwt
# 读取Excel
def read_excel(excel_name, sheet_name):
if excel_name and excel_name:
all_data_list = []
try:
workbook = xlrd.open_workbook(excel_name)
# sheets_name = workbook.sheet_names()
sheet_data = workbook.sheet_by_name(sheet_name)
rows = sheet_data.nrows
cols = sheet_data.ncols
#获取第一行数据
first_row_data = sheet_data.row_values(0)
for row_num in range(1,rows):
row_data = sheet_data.row_values(row_num)
dict_data = dict(zip(first_row_data, row_data))
all_data_list.append(dict_data)
return all_data_list
except:
print("文件名或表名有误!")
else:
print("文件名或表名有问题耶!")
# 写入表格
def write_excel(sheet_name, all_data_dict_list):
if sheet_name and all_data_dict_list:
handle = xlwt.Workbook()
sheet_one = handle.add_sheet(sheet_name, cell_overwrite_ok=False)
first_data = all_data_dict_list[0]
key_list = list(first_data.keys())
for i in range(0, len(key_list)):
row_0 = sheet_one.row(0)
row_0.write(i, key_list[i])
handle.save('E:/' + sheet_name + '.xls')
for i in range(1,len(all_data_dict_list)):
pass