通过 openpyxl 操作 excel 表格

博客地址:https://www.cnblogs.com/zylyehuo/

STEP1: 导入相关库

import os
from openpyxl import load_workbook

STEP2: 构建存放路径,将上传文件下载到服务器该路径下

excel_stus = request.FILES.get("excel_stus")  # 获取文件
path = os.path.join("media","files",excel_stus.name)  # 存放操作后的文件
with open(path,"wb") as f:
    for line in excel_stus:
        f.write(line)

STEP3: 通过python操作excel表格

加载某一个excel文件

wb = load_workbook(path)
# print("wb.sheetnames",wb.sheetnames)

获取sheet对象

worksheet = wb.worksheets[0]

获取单元格内容

for line in work_sheet.iter_rows(min_row=3):
    # print("line",line)
    # for cell in line:
    #     print(cell.value)
    print(line[索引值].value)  # 索引值从 0 开始
posted @ 2023-07-05 10:48  zylyehuo  阅读(19)  评论(0编辑  收藏  举报