Accessing Ranges

We’ve been calling the Value and Formula methods without really knowing what they refer to. They are methods of an object called a Range , which refers to a range of cells. You can use Ranges to get hold of regions of a spreadsheet in several ways:

>>> myRange1 = xlSheet.Cells(4,1)       # one-cell range
>>> myRange2 = xlSheet.Range("B5:C10")  # excel notation
>>> myRange3 = xlSheet.Range(xlSheet.Cells(2,2), xlSheet.Cells(3,8))
>>>

A sheet has a Range() method that returns a Range object. The Range method can accept the usual Excel notation or a pair of one-cell Range objects defining the top left and bottom right. You can even perform intersections and unions to build nonrectangular ranges. Once you have a Range object, as mentioned earlier, you have 84 methods and 72 properties to play with. These cover all the formatting options including data.

Get Python Programming On Win32 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.