如何使用openpyxl在excel中检测合并的单元格 [英] How to detect merged cells in excel with openpyxl
问题描述
我正在尝试从包含合并单元格的Excel工作表中读取数据. 当使用openpyxl读取合并的单元格时,第一个合并的单元格包含该值,其余单元格为空.
I'm trying to read data from excel sheet that contains merged cells. When reading merged cells with openpyxl the first merged cell contain the value and the rest of the cells are empty.
我想知道每个单元格是否合并以及合并了多少个单元格,但是我找不到任何可以合并的函数. 该工作表上有其他空白的单元格,所以我不能使用它.
I would like to know about each cell if it merge and how many cells are merged but I couldn't find any function that do so. The sheet have empty others cells, so I can't use that.
感谢您的帮助,我更喜欢openpyxl模块,但是其他模块也可以工作.
Will appreciate any help, I prefer openpyxl module but other modules can work,too.
谢谢大家!
推荐答案
您可以使用merged_cells.ranges
(merged_cell_ranges
在版本 merged_cells.ranges )(似乎无法每行查找),如下所示:
You can use merged_cells.ranges
(merged_cell_ranges
has been deprecated in version 2.5.0-b1 (2017-10-19), changed to merged_cells.ranges) on the sheet (can't seem to find per row) like this:
from openpyxl import load_workbook
wb = load_workbook(filename='a file name')
sheet_ranges = wb['Sheet1']
print(sheet_ranges.merged_cells.ranges)
这篇关于如何使用openpyxl在excel中检测合并的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!