** 一、安装xlrd模块:**
1、mac下打开终端输入命令:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">pip install xlrd</pre>
2、验证安装是否成功:
- 在mac终端输入 python 进入python环境
- 然后输入 import xlrd
不报错说明模块安装成功
二、常用方法:
1、导入模块:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">import xlrd</pre>
2、打开文件:
<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">x1 = xlrd.open_workbook("data.xlsx")</pre>
3、获取sheet:
- 获取所有sheet名字:x1.sheet_names()
- 获取sheet数量:x1.nsheets
- 获取所有sheet对象:x1.sheets()
- 通过sheet名查找:x1.sheet_by_name("test”)
- 通过索引查找:x1.sheet_by_index(3)
# -*- coding:utf-8 -*-
import xlrd import os filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 1、打开文件
x1 = xlrd.open_workbook(filePath) # 2、获取sheet对象
print 'sheet_names:', x1.sheet_names() # 获取所有sheet名字
print 'sheet_number:', x1.nsheets # 获取sheet数量
print 'sheet_object:', x1.sheets() # 获取所有sheet对象
print 'By_name:', x1.sheet_by_name("test") # 通过sheet名查找
print 'By_index:', x1.sheet_by_index(3) # 通过索引查找</pre>
4、获取sheet的汇总数据:
- 获取sheet名:sheet1.name
- 获取总行数:sheet1.nrows
- 获取总列数:sheet1.ncols
# -*- coding:utf-8 -*-
import xlrd import os from datetime import date,datetime
filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 打开文件
x1 = xlrd.open_workbook(filePath) # 获取sheet的汇总数据
sheet1 = x1.sheet_by_name("plan") print "sheet name:", sheet1.name # get sheet name
print "row num:", sheet1.nrows # get sheet all rows number
print "col num:", sheet1.ncols # get sheet all columns number</pre>
5、单元格批量读取:
a)行操作:
- sheet1.row_values(0) # 获取第一行所有内容,合并单元格,首行显示值,其它为空。
- sheet1.row(0) # 获取单元格值类型和内容
- sheet1.row_types(0) # 获取单元格数据类型
# -*- coding:utf-8 -*-
import xlrd import os from datetime import date,datetime
filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename)
x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan") # 单元格批量读取
print sheet1.row_values(0) # 获取第一行所有内容,合并单元格,首行显示值,其它为空。
print sheet1.row(0) # 获取单元格值类型和内容
print sheet1.row_types(0) # 获取单元格数据类型 </pre>
b) 表操作
- sheet1.row_values(0, 6, 10) # 取第1行,第6~10列(不含第10表)
- sheet1.col_values(0, 0, 5) # 取第1列,第0~5行(不含第5行)
- sheet1.row_slice(2, 0, 2) # 获取单元格值类型和内容
- sheet1.row_types(1, 0, 2) # 获取单元格数据类型
# -*- coding:utf-8 -*-
import xlrd import os from datetime import date,datetime
filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 1、打开文件
x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan") # 列操作
print sheet1.row_values(0, 6, 10) # 取第1行,第6~10列(不含第10表)
print sheet1.col_values(0, 0, 5) # 取第1列,第0~5行(不含第5行)
print sheet1.row_slice(2, 0, 2) # 获取单元格值类型和内容,同sheet1.row(0)
print sheet1.row_types(1, 0, 2) # 获取单元格数据类型</pre>
6、特定单元格读取:
a) 获取单元格值:
- sheet1.cell_value(1, 2)
- sheet1.cell(1, 2).value
- sheet1.row(1)[2].value
b) 获取单元格类型:
- sheet1.cell(1, 2).ctype
- sheet1.cell_type(1, 2)
- sheet1.row(1)[2].ctype
# -*- coding:utf-8 -*-
import xlrd import os from datetime import date,datetime
filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename)
x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan") # 特定单元格读取 # 取值
print sheet1.cell_value(1, 2) print sheet1.cell(1, 2).value print sheet1.row(1)[2].value #取类型
print sheet1.cell(1, 2).ctype print sheet1.cell_type(1, 2) print sheet1.row(1)[2].ctype</pre>
7、(0,0)转换A1:
- xlrd.cellname(0, 0) # (0,0)转换成A1
- xlrd.cellnameabs(0, 0) # (0,0)转换成1
- xlrd.colname(30) # 把列由数字转换为字母表示
# -*- coding:utf-8 -*-
import xlrd import os
filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) # 打开文件
x1 = xlrd.open_workbook(filePath)
sheet1 = x1.sheet_by_name("plan") # (0,0)转换成A1
print xlrd.cellname(0, 0) # (0,0)转换成A1
print xlrd.cellnameabs(0, 0) # (0,0)转换成$A$1
print xlrd.colname(30) # 把列由数字转换为字母表示</pre>
网友评论