python如何调用openpyxl库把数据写入Excel?

代码

import os
from openpyxl import load_workbook

project_path = os.path.abspath(os.path.join(os.getcwd(), "../../../"))  # 获取项目路径 根据实际改变
# *=======================获取excel=========================*
xlsx_path = f"{project_path}/report/sdkdemo_0928.xlsx"  # Excel的路径
work = load_workbook(xlsx_path)  # 获取到xlsx
print("读取执行表格为:", xlsx_path)
sheet = work["PerformanceDataSingle"]  # 获取到sheet表
# *=======================把数据写进表格=========================*
# A列 column=1 ; B列 column=2 ; ...G列 column=7 (基准值)
sheet.cell(row=2, column=8).value = 123 # 把123这个值写入第2行第8列
sheet.cell(row=3, column=8).value = 'abc'

# *=======================保存====================*
work.save(xlsx_path)  # 保存 写回到表格里
print("结束")

 

posted @ 2022-10-11 09:12  o蹲蹲o  阅读(46)  评论(0编辑  收藏  举报