网站首页 > 技术文章 正文
方法一:读excel文件单元格数据
import xlrd
book = xlrd.open_workbook('fruit.xlsx')
print('sheet页名称:',book.sheet_names())
sheet = book.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
print('该工作表有%d行,%d列.'%(rows,cols))
print('第三行内容为:',sheet.row_values(2))
print('第二列内容为%s,数据类型为%s.'%(sheet.col_values(1),type(sheet.col_values(1))))
print('第二列内容为%s,数据类型为%s.'%(sheet.col(1),type(sheet.col(1))))
print('第二行第二列的单元格内容为:',sheet.cell_value(1,1))
print('第三行第二列的单元格内容为:',sheet.cell(2,1).value)
print('第五行第三列的单元格内容为:',sheet.row(4)[2].value)
print('第五行第三列的单元格内容为%s,数据类型为%s'%(sheet.col(2)[4].value,type(sheet.col(2)[4].value)))
print('第五行第三列的单元格内容为%s,数据类型为%s'%(sheet.col(2)[4],type(sheet.col(2)[4])))
方法二:读excel文件单元格数据--openpyxl
import openpyxl
book = openpyxl.load_workbook('fruit.xlsx')
print('所有sheet页名称:',book.sheetnames)
sheet = book.worksheets[0]
sheet2 = book['Sheet1']
sheet3 = book[book.sheetnames[0]]
print('工作表名称:',sheet3.title)
rows = sheet.max_row
cols = sheet.max_column
print('该工作表有%d行,%d列.'%(rows,cols))
print('该工作表的的第三行第二列的单元格内容为:%.2f',(sheet.cell(3,2).value))
以下为行列生成器
print(sheet.rows,sheet.columns)
for col in sheet.columns:
print(col)
for row in sheet.rows:
for i in row:
print(i.value,end=' ')
print()
获取某一行或列的内容
for i in list(sheet.rows)[1]:
print(i.value,end=' ')
print()
for i in list(sheet.columns)[0]:
print(i.value,end=' ')
- 上一篇: 一日一技:使用Python读取Excel文件
- 下一篇: Python“三步”即可爬取,毋庸置疑
猜你喜欢
- 2025-05-23 如何在Python中保存文件?如何读取文件?示例代码
- 2025-05-23 python读取Excel表格
- 2025-05-23 OpenCV-Python教程:从视频文件、相机获取图像、写视频文件
- 2025-05-23 Dify Sandbox实现文件路径获取与Excel数据处理
- 2025-05-23 python中文件读写操作最佳实践——使用 os.path 进行路径操作
- 2025-05-23 Python 开发工程师必会的 5 个文件操作库
- 2025-05-23 开眼界!Python遍历文件可以这样做
- 2025-05-23 白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件
- 2025-05-23 使用python获取选择目录及子目录下所有文件
- 2025-05-23 失业程序员复习python笔记——excel读写处理
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)