openpyxl操作Excel文件
1.安装openpyxl第三方库
pip install openpyxl -i Python国内镜像源地址(百度内搜到,如果不能用协议名改为https)
或者永久更改pip包管理工具安装地址
pip/pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
然后 pip install 软件包名
2.openpyxl读取Excel文件
import openpyxl
wb = openpyxl.open('./files/data.xlsx')
names = wb.sheetnames
print(names)
sheet1 = wb.active
print(sheet1)
sheet2 = wb['原始数据']
print(sheet2)
m_r = sheet2.max_row
m_c = sheet2.max_column
print(m_r, m_c)
cell1 = sheet2.cell(2, 1)
cell2 = sheet2.cell(19, 3)
print(cell1.value, cell2.value)
3.灵活获取Excel文件中数据
![请添加图片描述](https://img-blog.csdnimg.cn/9e24ecf5ce1b491293642d7b88cb40e4.png)
import openpyxl
wb = openpyxl.load_workbook('files/data.xlsx')
sheet = wb['原始数据']
row = 4
for col in range(1, sheet.max_column+1):
cell = sheet.cell(row, col)
print(cell.value)
print('------------------------------华丽的分割线-----------------------------')
col = 2
for row in range(1, sheet.max_row+1):
cell = sheet.cell(row, col)
print(cell.value)
print('------------------------------华丽的分割线-----------------------------')
for row in range(1, sheet.max_row+1):
for col in range(1, sheet.max_column+1):
cell = sheet.cell(row, col)
print(cell.value)
print('------------------')
all_students = []
for row in range(2, sheet.max_row + 1):
stu = {}
for col in range(1, sheet.max_column+1):
data_cell = sheet.cell(row, col)
key_cell = sheet.cell(1, col)
stu.setdefault(key_cell.value, data_cell.value)
all_students.append(stu)
print(all_students)
4.openpyxl写操作Excel文件
import openpyxl
wb = openpyxl.Workbook()
wb1 = openpyxl.load_workbook('files/data.xlsx')
sheet1 = wb.create_sheet()
sheet2 = wb.create_sheet('student')
sheet3 = wb.create_sheet('teacher', 0)
if '电影信息' not in wb1.sheetnames:
sheet4 = wb1.create_sheet('电影信息')
if 'Sheet1' in wb1.sheetnames:
wb1.remove(wb1['Sheet1'])
sheet5 = wb1['原始数据']
sheet5.cell(1, 6).value = '平均分'
sheet5.cell(6, 2).value = '(缺考)'
sheet5.cell(2, 2).value = None
wb.save('files/data2.xlsx')
wb1.save('files/data.xlsx')
5.灵活写操作Excel文件
data = [
{'姓名': '李楠', '英语': 78, '办公软件操作': 96, '电子商务': 73, '计算机基础': 79},
{'姓名': '方鹏', '英语': 63, '办公软件操作': 94, '电子商务': 91, '计算机基础': 78},
{'姓名': '李磊', '英语': 89, '办公软件操作': 65, '电子商务': 0, '计算机基础': 0},
{'姓名': '王小若', '英语': 81, '办公软件操作': 77, '电子商务': 73, '计算机基础': 80},
{'姓名': '陈雨', '英语': 0, '办公软件操作': 87, '电子商务': 84, '计算机基础': 88},
{'姓名': '石璐', '英语': 91, '办公软件操作': 95, '电子商务': 84, '计算机基础': 80},
{'姓名': '张瑛', '英语': 73, '办公软件操作': 92, '电子商务': 92, '计算机基础': 89},
{'姓名': '程晓', '英语': 80, '办公软件操作': 86, '电子商务': 0, '计算机基础': 0},
{'姓名': '王丽', '英语': 72, '办公软件操作': 55, '电子商务': 88, '计算机基础': 70},
{'姓名': '赵军力', '英语': 69, '办公软件操作': 69, '电子商务': 63, '计算机基础': 76},
{'姓名': '王明', '英语': 96, '办公软件操作': 80, '电子商务': 85, '计算机基础': 64},
{'姓名': '李丽', '英语': 64, '办公软件操作': 0, '电子商务': 74, '计算机基础': 86},
{'姓名': '张帆', '英语': 72, '办公软件操作': 64, '电子商务': 97, '计算机基础': 74},
{'姓名': '张珊珊', '英语': 60, '办公软件操作': 95, '电子商务': 69, '计算机基础': 61},
{'姓名': '刘丽丽', '英语': 78, '办公软件操作': 93, '电子商务': 81, '计算机基础': 0},
{'姓名': '石节庆', '英语': 62, '办公软件操作': 63, '电子商务': 64, '计算机基础': 70},
{'姓名': '路瑶', '英语': 0, '办公软件操作': 0, '电子商务': 0, '计算机基础': 63},
{'姓名': '李贵明', '英语': 84, '办公软件操作': 90, '电子商务': 87, '计算机基础': 99}
]
data1 = [
{'name': '李楠', 'gender': '女', 'score': 1000, 'email': '726550822@qq.com'},
{'name': '赵军力', 'gender': '男', 'score': 567, 'email': 'y_t209@163.com'},
{'name': '张帆', 'gender': '男', 'score': 478, 'email': 'zhuucc@163.com'},
{'name': '王小若', 'gender': '女', 'score': 672, 'email': '873925431@qq.com'},
{'name': '李贵明', 'gender': '男', 'score': 325, 'email': '1873215638@qq.com'}
]
import openpyxl
import os
if os.path.exists('files/python数据分析.xlsx'):
wb = openpyxl.load_workbook('files/python数据分析.xlsx')
else:
wb = openpyxl.Workbook()
if '学生信息' in wb.sheetnames:
stu_sheet = wb['学生信息']
else:
stu_sheet = wb.create_sheet('学生信息')
contact_sheet = wb.create_sheet('学生联系方式')
col = 1
for key in data[0]:
stu_sheet.cell(1, col).value = key
col += 1
row = 2
for stu in data:
col = 1
for key in stu:
stu_sheet.cell(row, col).value = stu[key]
col += 1
row += 1
col = 1
for key in data1[0]:
contact_sheet.cell(1, col).value = key
col += 1
row = 2
for stu in data1:
col = 1
for key in stu:
contact_sheet.cell(row, col).value = stu[key]
col += 1
row += 1
wb.save('files/python数据分析.xlsx')