首页 > 编程语言 > 详细

python之读取excel实例演示

时间:2020-07-20 11:13:39      阅读:87      评论:0      收藏:0      [点我收藏+]

1、基础信息点击这里

import openpyxl

def read_excel(workbook,sheetname=None):
    wd=openpyxl.load_workbook(workbook)
    # 判断sheet是否有指定
    if not sheetname :
        sheet=wd.active
    else:
        sheet=wd[sheetname]
    aa=[]
    #按列遍历所有值
    for row in sheet.columns:
        chile=[cell.value for cell in row]
        aa.append(chile)
    return aa
def work_excel(excelname,data,sheetname=Sheet1):
    try:
        wd=openpyxl.Workbook()
        sheet=wd.active
        sheet.title=sheetname
        for row,item in enumerate(data):#带索引的遍历,索引值复制给了row
            for colum,ccc in enumerate(item):
                sheet.cell(row=row+1,column=colum+1,value=ccc)
        wd.save(excelname+.xlsx)
        return "写入成功"
    except BaseException as aa:
        print(aa)
        return "写入失败"



data=read_excel(stu_info2.xlsx)
# print(type(data))
# print(data)
print(work_excel(test,data=data))

 

python之读取excel实例演示

原文:https://www.cnblogs.com/jinbiaobowen/p/13343174.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!