Excel Object Model

The hierarchical system formed by abstracting Excel interface elements (such as workbooks, worksheets, cell ranges, etc.) into objects and combining them is called the Excel object model. With the Excel object model, Excel VBA or Python can program against these objects to achieve control and interaction with Excel. For example, you can programmatically open data files in an Excel worksheet or input data directly, process the data, and then write the results to the worksheet as data or charts and save them.

Excel Objects and Their Hierarchy

To facilitate script development for users, Excel provides many ready-made objects that have been encapsulated, such as the Application object (representing the Excel application), the Workbook object (representing a workbook), the Worksheet object (representing a worksheet), the Range object (representing a cell range), and the Chart object (representing a chart). They describe and replace real-world scenarios such as office work, files, forms, cell ranges in forms, and actual drawn charts. With these objects, we can use Python to control them programmatically.

The Excel object model shown in Figure 4-1 includes the Application object, Workbook object, Worksheet object, and Range object. They have a subordinate and inclusive relationship from top to bottom—to set an object at the next level, you must first obtain the object at the upper level. Workbooks is a collection containing all Workbook objects in the current Excel application, and the Worksheets collection object contains all Worksheet objects in the current workbook.

Figure 4-1 Excel Object Model