When working with Excel spreadsheets, cell merging is a common operation. Merging cells combines multiple adjacent cells into a larger cell, which is often used to create headers, highlight important information, or improve table layout. This article demonstrates how to merge and split Excel cells programmatically using Python.
Environment Setup
First, install the Spire.XLS library:
pip install Spire.XLS
This library provides comprehensive Excel document manipulation capabilities, including cell merging, splitting, and detection of merged cells.
Merging Cells
The merge operation combines a specified range of cells into a single cell. After merging, only the content of the top-left cell is preserved; content in other cells will be cleared.
Basic Merge Operation
from spire.xls import *
from spire.xls.common import *
# Create a workbook object
workbook = Workbook()
# Load an existing Excel file
workbook.LoadFromFile("input.xlsx")
# Get the first worksheet
sheet = workbook.Worksheets[0]
# Merge cells in the specified range
sheet.Range["A1:D1"].Merge()
# Save the file
workbook.SaveToFile("merged_cells.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
In this example, Range["A1:D1"] represents the cell range from A1 to D1, and the Merge() method merges all cells within this range into a single cell.
Merging Entire Column Cells
In addition to merging specified ranges, entire columns can also be merged:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
# Merge all cells in column 7
workbook.Worksheets[0].Columns[6].Merge()
workbook.SaveToFile("merged_column.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Note that column indices start from 0, so Columns[6] represents the 7th column (column G).
Splitting Cells
When it is necessary to unmerge cells, the split operation can be used. Splitting restores previously merged cells back to their original individual cells.
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("merged_file.xlsx")
# Get the worksheet
sheet = workbook.Worksheets[0]
# Split the specified cell (this cell must be part of a merged area)
sheet.Range["A1"].UnMerge()
workbook.SaveToFile("unmerged_cells.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
The UnMerge() method splits the merged area containing the specified cell. Simply specify any cell within the merged area to complete the split operation.
Detecting and Batch Processing Merged Cells
In practical applications, it may be necessary to detect all merged cells in a worksheet and perform batch processing.
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
# Get all merged cell ranges in the worksheet
merged_ranges = sheet.MergedCells
# Iterate through all merged areas and split them
for cell_range in merged_ranges:
cell_range.UnMerge()
workbook.SaveToFile("all_unmerged.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
The MergedCells property returns a collection of all merged cell ranges in the worksheet. By iterating through this collection, unified processing can be performed on all merged cells.
Practical Tips
Checking Cell Content Before Merging
When merging cells, only the content of the top-left cell is preserved. If there is important data in other cells, it is recommended to check or back up before merging:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
# Get the range to be merged
target_range = sheet.Range["A1:C1"]
# Check if there are non-empty cells within the range
has_data = False
for row in range(target_range.Row, target_range.Row + target_range.RowCount):
for col in range(target_range.Column, target_range.Column + target_range.ColumnCount):
cell = sheet.Range[row, col]
if cell.Value and cell.Value.strip():
has_data = True
break
if not has_data or input("There is data in the range. Continue merging? (y/n): ") == 'y':
target_range.Merge()
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Merging Multiple Discontinuous Ranges
Merge operations can be performed separately on multiple different cell ranges:
from spire.xls import *
from spire.xls.common import *
workbook = Workbook()
workbook.LoadFromFile("input.xlsx")
sheet = workbook.Worksheets[0]
# Merge multiple different ranges
sheet.Range["A1:D1"].Merge() # Merge header
sheet.Range["A3:A5"].Merge() # Merge left-side cells
sheet.Range["F8:H8"].Merge() # Merge bottom cells
workbook.SaveToFile("multiple_merged.xlsx", ExcelVersion.Version2013)
workbook.Dispose()
Summary
This article introduced basic methods for merging and splitting Excel cells using Python. The Merge() method can merge specified ranges of cells, the UnMerge() method can split merged cells, and the MergedCells property can retrieve all merged cell ranges for batch processing.
These operations are highly practical when creating reports, designing table layouts, or handling data presentation. Mastering cell merging and splitting techniques enables developers to more flexibly control the appearance and structure of Excel documents, thereby improving the efficiency of automated Excel file processing.

Top comments (0)