How To Reference a Cell Range Using OpenPyXL?

Method

  • Given the coordinates of the top-left and bottom-right corners of a range, use these coordinates to reference the cell range. – A tuple of cell objects.
  • Reference rows and columns – A tuple of cell objects.
  • Use the `CellRange` class.

Sample Code

#Referencing Cell Ranges

#Import load_workbook function
from openpyxl import Workbook

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

#Get a range of cells
cr=ws['A1:C4']
#Or
#cr=ws['A1':'C4']
print(cr)
print(cr[2][2].value)

wb.save('test.xlsx')
wb.close()

Leave a Reply

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