#1,启动excel
app = xw.App(visible=False, add_book=False)
wb = xw.Book(save_excel_path)
sht = wb.sheets[0]
#2,写入表头
sht.range('A1').value = 'num' # 向A1单元格写入该行数据lable 如:CK-1
sht.range('B1').value = 'file_new' # 向B1单元格写入该行数据lable 如:CK-1_1
sht.range('C1').value = 'r_min' # 向C1单元格写入该行数据lable 如:r_min
sht.range('D1').value = 'r_max' # 向D1单元格写入该行数据lable
sht.range('E1').value = 'g_min' # 向E1单元格写入该行数据lable
sht.range('F1').value = 'g_max' # 向F1单元格写入该行数据lable
sht.range('G1').value = 'b_min' # 向G1单元格写入该行数据lable
sht.range('H1').value = 'b_max' # 向H1单元格写入该行数据lable
sht.range('I1').value = 'r_mean' # 向i1单元格写入该行数据lable
sht.range('J1').value = 'g_mean' # 向j1单元格写入该行数据lable
sht.range('K1').value = 'b_mean' # 向k1单元格写入该行数据lable
#3,循环写入列
save_name = os.path.join('B' + str(j + 2)) #j为循环次数,从B2位置开始顺序存入列
sht.range(save_name).options(transpose=True).value = file_new # 从b2单元格开始存入
#4,循环写入行
save_num = os.path.join('C' + str(N+2) + ':' + 'K' + str(N+2)) #从C2:K2循环存放行,共存放32行数据(果实存放了32天)
sht.range(save_num).value = [r_min, r_max, g_min, g_max, b_min, b_max, r_mean, g_mean, b_mean] # 从C2:K2单元格开始存入一行
python在excel中遍历存储语句
于 2023-03-21 16:00:06 首次发布