
正文
python之读取excel实例演示
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
1、基础知识点击这里
import openpyxldef 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))






