python自动化办公(第6~7课)模块介绍,读取excel表格内容
Excel中的术语
- 打开Excel表格并获取工作表名称
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
print(workbook.sheetnames)
['sheet表1']
- 通过sheet名获取表格并获取表格的尺寸大小
- sheet=workbook[工作表名称]
- sheet.dimensions
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
sheet = workbook["sheet表1"]
print(sheet.dimensions)
print(type(sheet.dimensions))
A1:G1002
<class 'str'>
- 获取表格内某个格子的数据
- workbook.active 打开活跃的/唯一的表格
- sheet[“A1”] 获取A1格子
- cell.value 该格子的数据
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
sheet = workbook.active
cell1 = sheet["A1"]
cell2 = sheet["B5"]
print(cell1.value,cell2.value)
user_id 12515996043
- 获取某个格子的行数,列数,坐标
.coordinate 坐标
cell = sheet["B5"]
print(cell.row,cell.column,cell.coordinate)
5 2 B5
- 可用行数和列数定位某一个格子
- sheet.cell(row=行数,column=列数)
cell = sheet.cell(row=1,column=1)
print(cell.value)
user_id
- 获取一系列格子
- sheet[“A1:A5”]
- sheet[“A”] ——只要A列
- sheet[“A:C”] ——A到C列
- sheet[5] ——5行
- 输出的全是元组,可用foe循环将每一个格子取出来
cells = sheet["A1:A5"]
print(cells)
((<Cell 'sheet表1'.A1>,), (<Cell 'sheet表1'.A2>,), (<Cell 'sheet表1'.A3>,), (<Cell 'sheet表1'.A4>,), (<Cell 'sheet表1'.A5>,))
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
sheet = workbook.active
for row in sheet.iter_rows(min_row=2,max_row=3,min_col=1,max_col=2):
print(row)
(<Cell 'sheet表1'.A2>, <Cell 'sheet表1'.B2>)
(<Cell 'sheet表1'.A3>, <Cell 'sheet表1'.B3>)
- 返回的是每一行为一个元组
for col in sheet.iter_cols(min_row=2,max_row=3,min_col=1,max_col=2):
print(col)
(<Cell 'sheet表1'.A2>, <Cell 'sheet表1'.A3>)
(<Cell 'sheet表1'.B2>, <Cell 'sheet表1'.B3>)
- 返回的是每一列为一个元组
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
sheet = workbook.active
for row in sheet.iter_rows(min_row=2,max_row=3,min_col=1,max_col=2):
for cell in row:
print(cell)
<Cell 'sheet表1'.A2>
<Cell 'sheet表1'.B2>
<Cell 'sheet表1'.A3>
<Cell 'sheet表1'.B3>
作业:编写python程序,要求如下:
- 打开文件阿里云天池电商婴儿用户数据.xlsx
- 找到其中空着的格子
- 输出这些格子的坐标,如A1,C10等
- 空格子有:[共21个]
- E153,A278,E278,E293,C320,G339,B342
- E503,B527,E544,F554,C590,G597,A610
- F611,E666,E791,E816,E916,B958,G976
from openpyxl import load_workbook
workbook = load_workbook(filename="阿里云天池电商婴儿用户数据.xlsx")
sheet = workbook.active
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
E153,A278,E278,E293,C320,G339,B342,
E503,B527,E544,F554,C590,G597,A610,
F611,E666,E791,E816,E916,B958,G976,
总共21个