xlsxwriter插入图像和txt转excel
import xlsxwriter
import os
def deal_excel(dir_pth):
book = xlsxwriter.Workbook('res_1.xlsx')
images_sheet = book.add_worksheet(u'Images')
confidence_sheet = book.add_worksheet(u'Confidences')
for i in range(2, 50):
images_sheet.set_row(i, 200)
images_sheet.set_column('B:O', 20)
confidence_sheet.set_column('A:AG', 20)
item = os.walk(dir_pth).__next__()
root_path = item[0]
sub_dir_name_list = item[1]
sub_first_file_name = item[2][0]
sub_file_path = os.path.join(root_path, sub_first_file_name)
sub_dir_path_list = []
for i in sub_dir_name_list:
sub_dir_path = os.path.join(root_path, i)
sub_dir_path_list.append(sub_dir_path)
subdir_file_name_list = os.listdir(sub_dir_path_list[0])
subdir_file_name_list = ['ColorZ.bmp', 'CurvatureMinPart1.bmp', 'CurvatureMaxPart1.bmp', 'CurvatureRowPart.bmp','CurvatureColPart.bmp' , 'SlopeColorCol.bmp', 'SlopeColorRow.bmp']
for i in range(len(subdir_file_name_list)):
images_sheet.write(1, i+1, subdir_file_name_list[i])
for i in range(len(sub_dir_name_list)):
images_sheet.write(i+2, 0, sub_dir_name_list[i])
for i in range(len(sub_dir_name_list)):
for j in range(len(subdir_file_name_list)):
img_path = os.path.join(dir_pth, sub_dir_name_list[i], subdir_file_name_list[j])
images_sheet.insert_image(i + 2, j + 1, img_path, {'x_scale': 0.21, 'y_scale': 0.2})
f = open(sub_file_path, 'r', encoding='utf-8')
x = 0
while True:
line = f.readline()
if not line:
break
for i in range(len(line.split(','))):
item = line.split(',')[i]
confidence_sheet.write(x, i, item)
x += 1
f.close()
if __name__ == '__main__':
deal_excel(r"C:\Users\29939\Desktop\当前项目文件\res")