How To Expande and Shrink Specified Regions Using OpenPyXL?

Method

  • Expand using the `expand` method of the `CellRange` object

cr.expand(right=0,down=0,left=0,up=0)

  • using the `shrink` method of the `CellRange` object

cr.shrink(right=0,bottom=0,left=0,top=0)

 

Use the `size` attribute to view the changes before and after.

Sample Code

#Expanding and Shinking Cell Ranges

#Import load_workbook function
from openpyxl import Workbook

#Create a new workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

#Import CellRange function
from openpyxl.worksheet.cell_range import CellRange

#Create a cell range object
cr=CellRange('D5:F7')
#Output the size of the cell range
print(cr.size)

#Expanding
cr.expand(right=1,down=2,left=0,up=0)
print(cr.size)
#Shinking
#cr.shrink(right=1,bottom=2,left=0,top=0)
#print(cr.size)

wb.save('test.xlsx')
wb.close()
Expande and Shrink Specified Regions Using OpenPyXL

Leave a Reply

Your email address will not be published. Required fields are marked *