import openpyxl
def create_to_excel(wbname, data, sheetname='Sheet1', ):
"""
将制定的信息保存到新建的excel表格中;
:param wbname:
:param data: 往excel中存储的数据;
:param sheetname:
:return:
"""
print("正在创建excel表格%s......" % (wbname))
# wb = openpyxl.load_workbook(wbname)
# 如果文件不存在, 自己实例化一个WorkBook的对象;
wb = openpyxl.Workbook()
# 获取当前活动工作表的对象
sheet = wb.active
# 修改工作表的名称
sheet.title = sheetname
# 将数据data写入excel表格中;
print("正在写入数据........")
for row, item in enumerate(data): # data发现有4行数据, item里面有三列数据;
for column, cellValue in enumerate(item):
cell = sheet.cell(row=row + 1, column=column + 1, value=cellValue)
wb.save(wbname)
print("保存工作薄%s成功......." % (wbname))
def readwb(wbname, sheetname=None):
# 1. 加载工作薄
wb = openpyxl.load_workbook(filename=wbname)
# 2. 选择操作的工作表
sheet = wb.active
# 读取数据, 存储为python的数据结构
goodsInfo = []
for row in sheet.rows:
lines = [cell.value for cell in row]
goodsInfo.append(lines)
goodsInfo = [row[0:3]for row in goodsInfo if row[6]=='A']
for row in goodsInfo:
row.pop(1)
return goodsInfo
if __name__ == '__main__':
goodsInfo = readwb('doc/201901python学员博客整理情况.xlsx', 'Sheet1')
print(len(goodsInfo))
create_to_excel('doc/成绩为A的学院信息.xlsx', goodsInfo, 'A信息统计')
上述图片为表头