这个是判断是否是合并单元格的
from openpyxl import load_workbook
from openpyxl.cell import MergedCell
wb=load_workbook('1.xlsx')
ws=wb.active
# # 合并单元格
# ws.merge_cells('A1:C3')
# # 给合并后的单元格赋值
# ws['A1'] = "I love FishC.com!"
ws1=ws['A1:D5']
a=[]
for temp_cell in ws1:
for temp_cell2 in temp_cell:
print(temp_cell2)
if isinstance(temp_cell2, MergedCell):
a.append(temp_cell2.coordinate)
a.sort()
print(a)
# print(ws1.merged_cells)
wb.save("1.xlsx")
<Cell 'Sheet1'.A1>
<MergedCell 'Sheet1'.B1>
<MergedCell 'Sheet1'.C1>
<Cell 'Sheet1'.D1>
<MergedCell 'Sheet1'.A2>
<MergedCell 'Sheet1'.B2>
<MergedCell 'Sheet1'.C2>
<Cell 'Sheet1'.D2>
<MergedCell 'Sheet1'.A3>
<MergedCell 'Sheet1'.B3>
<MergedCell 'Sheet1'.C3>
<Cell 'Sheet1'.D3>
<Cell 'Sheet1'.A4>
<Cell 'Sheet1'.B4>
<Cell 'Sheet1'.C4>
<Cell 'Sheet1'.D4>
<Cell 'Sheet1'.A5>
<Cell 'Sheet1'.B5>
<Cell 'Sheet1'.C5>
<Cell 'Sheet1'.D5>
['A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'] |