Merging Excel cells is a common operation in Excel data processing and table design. For example, when creating table titles, multiple cells are often merged to enable the title to be displayed across columns, making it more eye-catching and beautiful. In addition, when data is classified, cells of the same category can be merged in order to make data of the same category more visually integrated and distinctive. This article will introduce howMerge specified rows, columns, or cell ranges in Excel via Python。
- Python merges specified rows in Excel
- Python merges specified columns in Excel
- Python merges specified cell ranges in Excel
Python Excel library installation: It needs to be used in this articlefor Python library. You can directly install it using the following pip command: (It is also possibleDownload the product packageThen install from the local path)
pip install
Python merges specified rows in Excel
- use()Method loads the Excel sample file.
- use[]Properties get the specified worksheet.
- Call[index].Merge()Method merges specified rows in Excel.
- use()Method saves the result file.
from import * from import * #Load Excel Documents workbook = Workbook() ("Example.xlsx") #Get the first worksheet sheet = [0] #Merge the first line [0].Merge() #Save the result document ("Merge .xlsx", ExcelVersion.Version2013) ()
Python merges specified columns in Excel
- use()Method loads the Excel sample file.
- use[]Properties get the specified worksheet.
- Call[index].Merge()Method merges the specified columns in Excel.
- use()Method saves the result file.
from import * from import * #Load Excel Documents workbook = Workbook() ("Example.xlsx") #Get the first worksheet sheet = [0] #Merge the first column [0].Merge() #Save the result document ("Merge columns.xlsx", ExcelVersion.Version2013) ()
Python merges specified cell ranges in Excel
In addition to merging specified rows or columns in Excel, you can also merge specified cell ranges. The steps are as follows:
- use()Method loads the Excel sample file.
- use[]Properties get the specified worksheet.
- Call[].Merge()Method merges the range of cells specified in Excel.
- use()Method saves the result file.
from import * from import * #Load Excel Documents workbook = Workbook() ("Example.xlsx") #Get the first worksheet sheet = [0] #Merge cells of B2:D5 ["B2:D5"].Merge() #Save the result document ("Merge the specified cells.xlsx", ExcelVersion.Version2013) ()
Reasonable use of the function of merging cells in Excel can make the table layout more beautiful and the data display clearer. The Python library also supports unmerging cells, as well as a variety of other operations on Excel workbooks, worksheets, rows, columns, and cells. For details, please refer to:
/Tutorials/Python/-for-Python/Program-Guide/