Python基于xlrd模块处理合并单元格
作者:安琪儿一直在 时间:2023-08-28 21:10:49
目的:
python能使用xlrd模块实现对Excel数据的读取,且按照想要的输出形式。
总体思路:
(1)要想实现对Excel数据的读取,需要用到第三方应用,直接应用。
(2)实际操作时候和我们实际平时打开一个文件进行操作一样,先找到文件-->打开文件-->定义要读取的sheet-->读取出内容。
Excel处理合并单元格:
已存在合并单元格如下:
xlrd中的 merged_cells 属性介绍:[code]import xlrd
import xlrd
workbook = xlrd.open_workbook('./data/test_data.xlsx')
sheet = workbook.sheet_by_name('Sheet1')
merged = sheet.merged_cells # 返回一个列表 起始行,结束行,起始列,结束列)
print(merged)
读取合并单元格中的某一个单元格的值编写成一个方法:
def get_merged_cell_value(row_index,col_index):
cell_value = None
for (rlow, rhigh, clow, chigh) in merged:
if (row_index >= rlow and row_index < rhigh):
if (col_index >= clow and col_index < chigh):
cell_value = sheet.cell_value(rlow, clow)
return cell_value
print( get_merged_cell_value(0,1) )
给出坐标,判断是否为合并单元格:
#方法参数为单元格的坐标(x,y),如果给的坐标是合并的单元格,输出此单元格是合并的,否则,输出普通单元格
def get_merged_cell_value(row_index,col_index):
for (rlow, rhigh, clow, chigh) in merged:
if (row_index >= rlow and row_index < rhigh and col_index >= clow and col_index < chigh):
print("此单元格是合并单元格")
else:
print("此单元格为普通单元格")
print( get_merged_cell_value(4,3) )
##读取第3列的所有数据,并进行降序排序
clox=3
list1=[]
for i in range(1,sheet.nrows):
cell_value=float(sheet.cell_value(i,clox))
list1.append(cell_value)
print(list1)
list1.sort()
list1.reverse()
print(list1)
来源:https://www.cnblogs.com/123anqier-blog/p/13234406.html
标签:Python,xlrd,模块,合并,单元格
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python如何实现API的调用详解
2023-12-26 18:24:07
![](https://img.aspxhome.com/file/2023/6/133416_0s.png)
python3 反射的四种基本方法解析
2023-10-25 16:04:39
详解使用Selenium爬取豆瓣电影前100的爱情片相关信息
2021-12-04 10:52:23
![](https://img.aspxhome.com/file/2023/3/115043_0s.jpg)
Go语言omitempty选项的实现
2024-04-25 15:12:40
PyTorch之nn.ReLU与F.ReLU的区别介绍
2021-01-19 04:47:24
JS Object.preventExtensions(),Object.seal()与Object.freeze()用法实例分析
2023-09-04 22:44:33
Jupyter安装链接aconda实现过程图解
2022-02-04 21:33:57
![](https://img.aspxhome.com/file/2023/4/134864_0s.png)
Python随机生成身份证号码及校验功能
2023-03-20 08:30:47
Django框架用户注销功能实现方法分析
2021-07-22 17:58:08
python PaddleSpeech实现婴儿啼哭识别
2023-08-22 22:25:31
Python3实现的Mysql数据库操作封装类
2024-01-20 23:57:36
chatgpt 1020 错误码成功解决的三种方案(推荐)
2023-02-03 17:33:28
![](https://img.aspxhome.com/file/2023/3/104963_0s.png)
使用python将图片改为灰度图或黑白图
2023-04-17 12:28:52
![](https://img.aspxhome.com/file/2023/4/72114_0s.jpg)
python得到qq句柄,并显示在前台的方法
2021-10-08 12:44:30
Python实现清理微信僵尸粉功能示例【基于itchat模块】
2021-10-29 20:45:46
![](https://img.aspxhome.com/file/2023/1/90911_0s.png)
关于Python排序sort()函数和sorted()函数
2023-10-31 01:32:14
Python之ascii转中文的实现
2023-08-24 19:39:12
![](https://img.aspxhome.com/file/2023/0/62600_0s.png)
oracle常用sql语句
2010-07-23 13:27:00
从理论角度讨论JavaScript闭包
2024-03-29 06:46:46
Python性能优化的20条建议
2021-05-20 15:24:12