#纵向合并单元格取左上角的值为自己的值,横向合并单元格除了左上角单元格外,保持None为自己的值
import openpyxl
from openpyxl.cell import MergedCell
if __name__ == "__main__":
wb = openpyxl.load_workbook("test.xlsx")
sheet = wb["Sheet1"]
all_data=[]
for column in sheet.columns:
cellnew = []
for cell1 in column:
if isinstance(cell1, MergedCell):
for merged_range in sheet.merged_cells.ranges[:]:
if (cell1.coordinate in merged_range):
if(cell1.column==merged_range.min_col):
cellvalue = sheet.cell(row=merged_range.min_row, column=merged_range.min_col).value
cellnew.append(cellvalue)
print("纵向合并单元格:",cell1.value)
else:
cellnew.append(cell1.value)
print("横向合并单元格:", cell1.value)
else:
cellnew.append(cell1.value)
print("非合并单元格:", cell1.value)
all_data.append(cellnew)
print(all_data)
openpyxl合并单元格处理实例