【python办公自动化(6期)】6.python打开及读取Excel表格内容

╄→гoц情女王★ 提交于 2020-03-08 07:07:05

python打开及读取Excel表格内容
模块的介绍和安装
openpyxl模块
可以读取和写入Excel文件;需要单独安装;不包含在python标准库里;可以处理Excel数据、公式、样式,在表格里面插入图表等

pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple

在windows系统下使用此条指令进行第三方库的安装省时间
Excle表格术语
列: column,以字母表示,在表格的上方
行: row,以数字表示,但是注意这里是从1开始,在表格的左侧
表单: sheet,在表格的下部
打开Excel表格并获取表格名称
load_workbook(filename = 表格文件路径);workbook.sheetnames 获取表格文件内的sheet名称
注意:只能打开存在的表格,不能用该方法创建一个新表格文件

import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')

from openpyxl import load_workbook

workbook = load_workbook(filename= 'file1.xlsx')
print(workbook.sheetnames)
['Sheet1', 'Sheet2', 'Sheet3']

获得Excel里面的sheet之后就可以对表格的数据进行操作。比如:获取表格的尺寸大小;第一行第一列的数据,代码如下

import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')

from openpyxl import load_workbook

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
print(sheet.dimensions) 
cell = sheet['A1']
print(cell.value) 
A1:B1
as

获取某个格子的行数、列数、坐标
.row/.column/.coordinate

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
cell = sheet['A1']
print(cell.row, cell.column, cell.coordinate)

–> 输出结果为:1 1 A1
指定具体的行数和列数的格子
sheet.cell(row = 行数, column = 列数)

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
cell = sheet.cell(row = 1, column = 1)
print(cell.row, cell.column, cell.coordinate)

获取一系列格子
比如:sheet[‘A’]/sheet[2],字符就是列,数字就是行

import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')

from openpyxl import load_workbook

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
cell1 = sheet['A']
cell2 = sheet[2]
print(cell1, cell2)

输出结果:

(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>, 
<Cell 'Sheet1'.A7>, <Cell 'Sheet1'.A8>, <Cell 'Sheet1'.A9>, <Cell 'Sheet1'.A10>, <Cell 'Sheet1'.A11>, <Cell 'Sheet1'.A12>) (<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.D2>, <Cell 'Sheet1'.E2>, <Cell 'Sheet1'.F2>, <Cell 'Sheet1'.G2>, <Cell 'Sheet1'.H2>, <Cell 'Sheet1'.I2>, <Cell 'Sheet1'.J2>)

指定行和列的范围
按行获取
.iter_rows(min_row = 最低行数,max_row = 最高行数,min_col = 最低列数,max_col = 最高列数)

import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')

from openpyxl import load_workbook

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
for row in sheet.iter_rows(min_row=2, max_row=3, min_col= 1,max_col=2):
    print(row)

输出结果:

(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>)
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>)

按列获取
.iter_cols(min_row = 最低行数,max_row = 最高行数,min_col = 最低列数,max_col = 最高列数)

import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')

from openpyxl import load_workbook

workbook = load_workbook(filename= 'file1.xlsx')
sheet = workbook['Sheet1']
for col in sheet.iter_cols(min_row=2, max_row=3, min_col= 1,max_col=2):
    print(col)

输出结果:

(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>)
(<Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>)

迭代整个表格的所有行
.rows

for row in sheet.rows:
    print(row)

迭代整个表格的所有列
.columns

for col in sheet.columns:
    print(col)

注意这里和上面的区别,迭代全部的行和列的字符都是全拼,但是行的复数单词的rows,所以简写,全拼是一样的

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!