写入
import openpyxl
wb=openpyxl.Workbook() ##创建工作簿实例
ws=wb.active ##创建工作表实例
for i in range(1,10): ##row为行,column为列进行写入
ws.cell(row=i,column=1,value=i)
ws.cell(row=i, column=2, value='user{}'.format(i))
ws.cell(row=i, column=3, value='男')
wb.save('user.xlsx') ##保存文件
wb.close()
写入后打开文件
读取
wb=openpyxl.load_workbook('user.xlsx',read_only=True) ##读取excel文件,实例化工作簿
ws=wb.active ##实例化工作表
rows=ws.max_row ##获取最大行数和列数
columns=ws.max_column
dates=[]
for i in range(1,rows+1): ##遍历所有行,列表套字典表的方式将数据存储至dates中
date={'index':ws.cell(row=i,column=1).value,'name':ws.cell(row=i,column=2).value,'gender':ws.cell(row=i,column=3).value}
dates.append(date)
print(dates)
wb.close()
打印后显示内容