1、当前文件不存在指定的子文件夹则创建
2、文件存在追加写入
3、文件不存在创建文件并写入表头
# -*- coding: utf-8 -*-
import openpyxl as xl
import osdef write_excel_file(folder_path):if not os.path.exists(folder_path):os.makedirs(folder_path)result_path = os.path.join(folder_path, "my.xlsx")print('***** 开始写入excel文件 ' + result_path + ' ***** \n')# 文件存在则加载,不存在创建if os.path.exists(result_path):print('***** excel已存在,在表后添加数据 ' + result_path + ' ***** \n')workbook = xl.load_workbook(result_path)else:print('***** excel不存在,创建excel ' + result_path + ' ***** \n')workbook = xl.Workbook()workbook.create_sheet('订单信息')workbook.create_sheet('订单详情')workbook.remove(workbook['Sheet']) #删除空白页workbook.save(result_path)sheet = workbook['订单详情']headers = ["URL", "predict", "score"]sheet.append(headers)sheet = workbook['订单详情']result = [['1', 1, 1], ['2', 2, 2], ['3', 3, 3]]for data in result:sheet.append(data)workbook.save(result_path)print('***** 生成Excel文件 ' + result_path + ' ***** \n')if __name__ == '__main__':write_excel_file("2023-08-28\\")