excel中判断指定的单元格是否是合并单元格

    data = xlrd.open_workbook(r"./demo.xlsx")
    # 获取sheet
    table = data.sheet_by_name('Vehicle RTM')

下面函数为将第lie_detection列的合并单元格的开始和结束放到colspan字典中。

def cell_detection(lie_detection):
	# 计算出合并的单元格有哪些
	colspan = {}
	if table.merged_cells:
		for item in table.merged_cells:
	        	for row1 in range(item[0], item[1]):
				for col1 in range(item[2], item[3]):
					if col1 == lie_detection:
						if row1 > 3:
							# 合并单元格的首格是有值的,所以在这里进行了去重
							if (row1, col1) != (item[0], item[2]):
								colspan.update({(row1, col1): (item[0], item[2])})
	return colspan

如何确定合并行以及最大行值请看上篇博客字典的用法。

发布了26 篇原创文章 · 获赞 34 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_36662437/article/details/100043449