python xlrd使用

在pycharm下面的terminal里输入pip install xlrd 就可以安装了
python的话如下:

import os
os.system("cmd")#运行,打开后输入pip install xlrd

下面是sheet_1.sheet_2的数据了。
在这里插入图片描述
在这里插入图片描述

import xlrd
book = xlrd.open_workbook(r'C:\Users\top\AppData\Local\Kingsoft\WPS Cloud Files\userdata\qing\filecache\,,,的云文档\工作簿1.xlsx')
#文件位置去属性里面复制,确保正确。
sheet = book.sheet_by_index(1)#对应的是sheet_2,如果把一改为0,对应sheet_1
for row_index in range(sheet.nrows):
	print(sheet.row_values(row_index))#输出sheet_2的所有数据
for row_index in range(1, 5):#输出2到6行的数据
	print(sheet.row_values(row_index, 1, 6))#输出该行2到7列的数据
print(book.sheet_names())
print(book.sheet_loaded(0))#判断sheet_1是否导入,如果是1则表示sheet_2是否导入,也可以直接写名字,如果导入了,返回True,否则无返回值
print('列数:{}行数:{}'.format(sheet.ncols,sheet.nrows))
print(sheet.row(0))
print(sheet.row_slice(0))#与上一行代码相同,返回对应索引的数据,0——12分别对应第一行第二行......
print(sheet.row_types(0))#返回由该行中所有单元格的数据类型组成的列表,0——12分别对应第一行第二行......
print(sheet.row_values(2))#返回该行的数据,0——12分别对应第一行第二行......
print(sheet.cell(2,2))#跟坐标差不多,(0,0)表示第一行第一列
#12,13,14行的代码里面的row可以换为col(输出对应列),cell(输出对应单元格)

输出:

C:\Users\top\PycharmProjects\untitled4\venv\Scripts\python.exe C:/Users/top/PycharmProjects/untitled4/venv/a_10.py
['姓名', '语文', '数学', '英语', '生物', '化学', '物理']
['小李', 12.0, 1.0, 1.0, 1.0, 1.0, 1.0]
['小米', 245.0, 276.0, 92.0, 62.0, 27.0, 22.0]
['小明', 332.0, 53.0, 873.0, 33.0, 433.0, 4563.0]
['小鸟', 4.0, 4.0, 4.0, 4.0, 4.0, 4.0]
['小龙', 5.0, 5.0, 5.0, 5.0, 5.0, 5.0]
['小王', 6.0, 6.0, 6.0, 6.0, 6.0, 6.0]
['小哥', 7.0, 7.0, 7.0, 7.0, 7.0, 7.0]
['小樱', 8.0, 8.0, 8.0, 8.0, 8.0, 8.0]
['小胖', 9.0, 9.0, 9.0, 9.0, 9.0, 9.0]
['小芳', 10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
['小任', 11.0, 11.0, 11.0, 11.0, 11.0, 11.0]
['小钱', 12.0, 12.0, 12.0, 12.0, 12.0, 12.0]
[12.0, 1.0, 1.0, 1.0, 1.0]
[245.0, 276.0, 92.0, 62.0, 27.0]
[332.0, 53.0, 873.0, 33.0, 433.0]
[4.0, 4.0, 4.0, 4.0, 4.0]
['Sheet1', 'Sheet2']
True
列数:7行数:13
[text:'姓名', text:'语文', text:'数学', text:'英语', text:'生物', text:'化学', text:'物理']
[text:'姓名', text:'语文', text:'数学', text:'英语', text:'生物', text:'化学', text:'物理']
array('B', [1, 1, 1, 1, 1, 1, 1])
['小米', 245.0, 276.0, 92.0, 62.0, 27.0, 22.0]
number:276.0

Process finished with exit code 0

原创文章 382 获赞 114 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45949073/article/details/105790394