zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python 第三方包openpyxl 实现Excel读写操作

PythonExcel 实现 第三方 读写操作 openpyxl
2023-09-11 14:22:10 时间

python 第三方包openpyxl 实现Excel读写操作

from openpyxl import load_workbook

def read_cases(self,path):
    wk=load_workbook(path)
    sheet=wk['Sheet1']
    # r=sheet.rows[0]
    row1 = [item.value for item in list(sheet.rows)[0]]
    # print(row1)
    # print(sheet.max_row)  #获取总行数
    # print(sheet.max_column)  #获取总列数
    case_list=[]
    for i in range(1,sheet.max_row):
        case_list.append([item.value for item in list(sheet.rows)[i]])  #把一行的内容转换成list

    # print(case_list)
    # print(len(case_list))
    return case_list


    # xl_sheet_names=wk.get_sheet_names()#获取所有sheet页名字
    # print(xl_sheet_names)#打印所有sheet页名称
    # xl_sheet=wk.get_sheet_by_name(xl_sheet_names[0])#定位到相应sheet页,[0]为sheet页索引
    # xl_cell=sheet.cell(row=1,column=1).value#获取单元格值,row和column下标从1开始,
    # print(xl_cell)
    # xl_cell=sheet.cell(row=0,column=0,value="金额")#单元格赋值
    # filepath="D:/a.xlsx"#保存excel
    # wk.save(filepath)