Python Program to Merge Excel Cells using openpyxl
In this article, we will discuss how to merge cells in an excel sheet using Python. We will use openpyxl module for merging cells.
Installation of openpyxl using pip
pip install openpyxl
Implementation Of Merging Cells using openpyxl
Openpyxl provides a merge_cells() function for merging cells. When we merge cells, the top cell will be deleted from the worksheet. Openpyxl also provides an unmerged_cells() method for separating cells.
merge_cells(range_string=None, start_row=None, start_column=None, end_row=None, end_column=None)
# import library from openpyxl import Workbook from openpyxl.styles import Alignment work_book = Workbook() work_sheet = work_book.active # cells to merge work_sheet.merge_cells('A1:D3') cell = work_sheet.cell(row=1, column=1) # value of cell cell.value = 'quick fox jumps over the lazy dog' # aligment of data in cell cell.alignment = Alignment(horizontal='center', vertical='center') # save the workbook work_book.save('Excel_merge_cell.xlsx')