openpyxl: 表格excel的写入、追加以及sheet的添加

if epoch==1:
      excel.write_sheet_add(save_path, 'metric', [list(cur_metric.keys())])
excel.write_excel_append(save_path, 'metric', [list(cur_metric.values())])

import openpyxl

def write_sheet(path, sheet_name, value):
    index = len(value)
    workbook = openpyxl.Workbook(write_only=False)
    workbook.create_sheet(title=sheet_name, index=0)
    sheet = workbook.get_sheet_by_name(sheet_name)
    for i in range(0, index):
        sheet.append(value[i])
    workbook.save(path)

def write_excel_append(path, sheet_name, value):
    index = len(value)
    workbook = openpyxl.load_workbook(path)
    worksheet = workbook[sheet_name]
    for i in range(0, index):
        worksheet.append(value[i])
    workbook.save(path)

def write_sheet_add(path, sheet_new, value):
    index = len(value)
    workbook = openpyxl.load_workbook(path)
    workbook.create_sheet(sheet_new)
    worksheet_new = workbook[sheet_new]
    for i in range(0, index):
        worksheet_new.append(value[i])
    workbook.save(path)

你可能感兴趣的:(excel)