相关库
读:xlrd
写:xlwt
案例
要求:
将图1中的数据导以图2的形式写入另一个文件中
第一列索引关系:{1:K1-B1,2:K1-B2}
思路:
按行读取数据,根据索引关系按列写入数据
代码:
1 import xlrd
2 import xlwt
3 #柱号映射关系
4 index = ['K1-B1','K1-B2']
5 load_cnt = 6
6 #读excel文件
7 wb = xlrd.open_workbook(r'C:\Users\Administrator\Desktop\demo\111.xls')
8 sheet1 = wb.sheet_by_index(0)
9 index_col = sheet1.col_values(0)[1:]
10 joint_cnt = len(index_col)
11 #写excel文件
12 f = xlwt.Workbook()
13 sheet2 = f.add_sheet(u'sheet1',cell_overwrite_ok=True)
14 for i in range(joint_cnt):
15 for j in range(joint_cnt):
16 if index_col[j] == index[i]: break
17 target_row = sheet1.row_values(j+1)[1:]
18 num_cnt = len(target_row)
19 for k in range(load_cnt):
20 sheet2.write(3*i,k,target_row[3*k])
21 sheet2.write(3*i+1,k,target_row[3*k+1])
22 sheet2.write(3*i+2,k,target_row[3*k+2])
23 f.save(r'C:\Users\Administrator\Desktop\demo\222.xls')
参考
xlrd帮助
https://blog.csdn.net/Sukie_csdn/article/details/78672626
openpyxl帮助
https://segmentfault.com/a/1190000016256490
常用操作实现
https://blog.csdn.net/paul0127/article/details/82529153