参考博客python实现将数据写入Excel文件中_前端技术的博客-CSDN博客_python写excel
代码:
path = './'
img_path = './'
import os
import xlwt
import xlsxwriter.worksheet as sheet
book = xlwt.Workbook(encoding='utf-8', style_compression=0) # 创建excel表格类型文件
sheet = book.add_sheet('1', cell_overwrite_ok=True) # 在excel表格类型文件中建立一张sheet表单
col = ('获得奖项', '图片') # 自定义列名
for i in range(0, 1): # 写入列名
sheet.write(i, 0, col[i])
i = 1
for root, dirs, files in os.walk(path):
files.sort()
for name in files:
if name.split('.')[1] != 'jpg':
continue
data = name[:-4]
sheet.write(i, 0, data) # 写入列
# sheet.insert_image(i, 1, img_path)
i = i + 1
savepath = './1.xls' # 保存excel
book.save(savepath)