Python中load_workbook用法

模块功能:通过读取excel文件,操作excel数据

1.模块导入

from openpyxl import load_workbook 

2.加载excel表格

表格示例:

wb = load_workbook("学生.xlsx") #表格实例

3.获取表单sheet

sheet = wb['一班']

4.获取行数与列数

# 获取行数,中间有空行也计算
row_num = sheet.max_row
# 获取列数,中间有空列也计算 col_num
= sheet.max_column

5.获取单元格内容

# 1.
a1 = sheet['A1'].value
# 2.
a2 = sheet.cell(1,2).value

6.按行/列访问数据

lit = []
for row in sheet.rows:
    for col in row:
        lit.append(col.value)

lit1 = []
for col in sheet.columns:
    for row in col:
        lit1.append(row.value)

 

posted @ 2023-02-01 11:44  Lucky-qq  阅读(1233)  评论(0编辑  收藏  举报