Merge and Unmerge Excel Cells using openpyxl in Python
Last Updated :
28 Sep, 2024
In this article, we are going to learn about Python programs to merge and unmerge excel cells using openpyxl.
Introduction
Openpyxl is a Python library commonly used for reading and writing Excel Files. In this article, we will be using this library to merge cells in Excel. Merging is helpful when we want to display some special information in order to gain user attention or to just properly display a long word properly. For example, Consider the following sheet. If you want to display a long title. By default, it will appear like this.
Here, we can see that before merging, the title appears to be meaningless and less user-friendly. However, if we merge some cells, the heading will appear much more clearly and read-friendly. After merging some adjacent cells, It will look something like this.
In this article, we are going to accomplish this task using Python.
Merging cells in excel using Python
Step 1: Installing the required packages, we are using openpyxl to handle tasks such as the creation of a new workbook, updating an existing workbook, querying results, sorting, filtering, etc. Firstly let’s install the package by running the below command in cmd or terminal.
pip install openpyxl
Now, we can import the package into our Python program as follows:
import openpyxl
import os # for reading the excel file.
Step 2: Load an existing workbook to work on.
In this step, we are going to select the existing workbook we want to work on. For this, we need the location where the workbook is present in the local system. Let’s store this path in a PATH variable. If the excel file is present in the current working directory, we simply need to get the path of the current directory and join it with the name of our Excel file as follows:
path=os.path.join(os.getcwd(),"sample.xlsx");
Now, let’s load the workbook using the library function load_workbook from openpyxl. This function requires the path(from where the workbook has to be loaded) as defined above.
wb=openpyxl.load_workbook(path)
Now, let’s open the active worksheet from our workbook wb as follows:
ws=wb.active
Or, if we want to open a non-active worksheet. We can open it like this:
ws=wb["name_of_the_worksheet_here"]
Here, is the workbook, before merging the cells.
Step 3: Now, let’s apply the function merge cells to merge specific cells into one. This function requires the following arguments.
- start_row: Specifies the starting index of the row to start merging from.
- start_column: Specifies the starting index of the row to start merging from.
- end_row: Specifies the last index of the row to end merging.
- end_column: Specifies the last index of the column to end merging.
Now, let’s use the function to merge rows from 1 to 2 and columns from B to E into a single cell.
ws.merge_cells(start_row=1, start_column=2, end_row=2, end_column=5)
After this step, row number 1 to 2 and column number B to E will be merged into a single cell.
Step 4: Saving the workbook to apply the changes.
wb.save(filename="sample.xlsx")
Final Code:
Python
import openpyxl
import os
path=os.path.join(os.getcwd(),"sample.xlsx")
wb=openpyxl.load_workbook(path)
ws=wb.active
ws.merge_cells(start_row=1, start_column=2, end_row=2, end_column=5)
wb.save(filename="sample.xlsx")
Output:
Unmerging the cells in an excel file using Python
Step 1: Import the required packages.
import openpyxl
import os # for reading the excel file.
Step 2:Open the Excel Working using the load_workbook function from openpyxl.This function accepts a path as a parameter and opens the Excel File.
Here, we are first storing the path of the Excel File in a PATH variable. The name of our Excel File is sample.xlsx and it is presenting the current working directory. So we do something like this:
path=os.path.join(os.getcwd(),"sample.xlsx")
Now, let’s load the workbook into our Python code. and open the currently active workbook from it.
wb=openpyxl.load_workbook(path)
ws=wb.active
Step 3:Unmerging the cells.
As we can see initially the worksheet looks like this:
Here, the cells B1:E2 are merged into a single cell. Let’s unmerge these cells.
To do so, we use the unmerge_cells function from openpyxl which takes the following parameters:
1.start_row: Specifies the starting index of the row to start unmerging from.
2.start_column: Specifies the starting index of the row to start unmerging from.
3.end_row: Specifies the last index of the row to end unmerging.
4.end_column: Specifies the last index of the column to end unmerging.
Now, let’s use the function to unmerge row number 1 to 2 and column number 2 to 5 into individual cells.
ws.unmerge_cells(start_row=1, start_column=2, end_row=2, end_column=5)
After this step, the following worksheet will be produced.
Step 4:To save the changes, we can save the worksheet as:
wb.save(filename="sample.xlsx")
Final Code:
Python
import openpyxl
import os
path=os.path.join(os.getcwd(),"sample.xlsx")
wb=openpyxl.load_workbook(path)
ws=wb.active
ws.unmerge_cells(start_row=1, start_column=2, end_row=2, end_column=5)
wb.save(filename="sample.xlsx")
Output:

Similar Reads
Reading an excel file using Python openpyxl module
Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The Openpyxl Module allows Python programs to read and modify Excel files. For example, users might have to go through thousands of rows and pick out a few handfuls of information to make small cha
3 min read
Python | Writing to an excel file using openpyxl module
Prerequisite : Reading an excel file using openpyxl Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files. For example, user might have to go through thousands of rows and pick o
3 min read
Python | Adjusting rows and columns of an excel file using openpyxl module
Prerequisites : Excel file using openpyxl writing | reading Set the height and width of the cells:Worksheet objects have row_dimensions and column_dimensions attributes that control row heights and column widths. A sheetâs row_dimensions and column_dimensions are dictionary-like values; row_dimensio
3 min read
Python | Plotting charts in excel sheet using openpyxl module | Set - 1
Prerequisite: Reading & Writing to excel sheet using openpyxl Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Let's see how to plot different charts using realtime data. Charts are compo
6 min read
Python | Plotting charts in excel sheet using openpyxl module | Set â 2
Prerequisite: Python | Plotting charts in excel sheet using openpyxl module | Set â 1 Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Charts are composed of at least one series of one or mor
6 min read
Python | Plotting charts in excel sheet using openpyxl module | Set 3
Prerequisite : Plotting charts in excel sheet using openpyxl module Set - 1 | Set â 2Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Charts are composed of at least one series of one or more
7 min read
Adding Conditional Formatting to Excel Using Python Openpyxl
Adding conditional formatting using openpyxl is an easy and straightforward process. While working with Excel files, conditional formatting is useful for the visualization of trends in the data, highlighting crucial data points, and making data more meaningful and understandable. In this article, we
5 min read
How to delete one or more rows in excel using Openpyxl?
Openpyxl is a Python library to manipulate xlsx/xlsm/xltx/xltm files. With Openpyxl you can create a new Excel file or a sheet and can also be used on an existing Excel file or sheet. Installation This module does not come built-in with Python. To install this type the below command in the terminal.
5 min read
Formatting Cells using openpyxl in Python
When it comes to managing Excel files programmatically, Python offers a powerful tool in the form of the openpyxl library. This library not only allows us to read and write Excel documents but also provides extensive support for cell formatting. From fonts and colors to alignment and borders, openpy
4 min read
How to get sheet names using openpyxl - Python
The openpyxl library is widely used to handle excel files using Python. Often when dealing with excel files one might have noticed multiple sheets. The sheets are normally mentioned in the bottom left corner of the screen. One can install this library by running the following command. pip install op
2 min read