{"id":2284,"date":"2026-09-02T16:09:15","date_gmt":"2026-09-02T08:09:15","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2284"},"modified":"2026-03-28T12:19:36","modified_gmt":"2026-03-28T12:19:36","slug":"how-to-use-worksheetoleobjects-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetoleobjects-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.OLEObjects in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>OLEObjects<\/strong> member of the <strong>Worksheet<\/strong> object in Excel represents a collection of all OLE objects (such as embedded documents, ActiveX controls, or other insertable objects) on a specific worksheet. In xlwings, this collection is accessible via the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This allows for programmatic control over embedded objects, including enumeration, modification, and interaction with OLE controls within a workbook.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary use of the OLEObjects collection in xlwings is to manage embedded OLE objects. You can count the objects, retrieve a specific object by its index or name, modify properties (e.g., size, placement), or even invoke methods associated with ActiveX controls. This is particularly useful for automating dashboards or forms that contain interactive elements like buttons, list boxes, or embedded charts from other applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>Accessing the OLEObjects collection in xlwings follows the pattern: <code>worksheet.api.OLEObjects<\/code>. This returns a collection object. To reference a specific OLE object, you can use:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>worksheet.api.OLEObjects(Index)<\/code> where <code>Index<\/code> is the object&#8217;s numeric position (1-based) or its name as a string.<\/li>\n\n\n\n<li><code>worksheet.api.OLEObjects.Item(Index)<\/code> which is functionally equivalent.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Common properties and methods of an OLE object (returned from the collection) include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Name<\/code><\/strong>: Gets or sets the object&#8217;s name.<\/li>\n\n\n\n<li><strong><code>Left<\/code><\/strong>, <strong><code>Top<\/code><\/strong>, <strong><code>Width<\/code><\/strong>, <strong><code>Height<\/code><\/strong>: Control the object&#8217;s position and dimensions in points.<\/li>\n\n\n\n<li><strong><code>Object<\/code><\/strong>: Provides access to the underlying OLE object&#8217;s native interface (e.g., for an ActiveX control, you can access its specific properties).<\/li>\n\n\n\n<li><strong><code>Delete()<\/code><\/strong>: Removes the object from the worksheet.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>List all OLE objects on a worksheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('workbook.xlsx')\nws = wb.sheets&#91;'Sheet1']\nole_objects = ws.api.OLEObjects\ncount = ole_objects.Count\nprint(f\"Number of OLE objects: {count}\")\nfor i in range(1, count + 1):\n    obj = ole_objects(i)\n    print(f\"Object {i}: Name='{obj.Name}', Type='{obj.progID}'\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Resize and reposition an OLE object by name:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Assume an embedded Word document object named \"DocObject1\"\nobj = ws.api.OLEObjects(\"DocObject1\")\nobj.Left = 100 # Points from left edge\nobj.Top = 50 # Points from top\nobj.Width = 200\nobj.Height = 150<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Interact with an ActiveX command button:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Access the button, then its specific properties via the Object property\nbutton = ws.api.OLEObjects(\"CommandButton1\")\n# Change the button caption\nbutton.Object.Caption = \"Click Me\"\n# The Object property exposes the ActiveX control's native interface<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Delete all OLE objects on a sheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ole_objects = ws.api.OLEObjects\nfor i in range(ole_objects.Count, 0, -1): # Iterate backwards when deleting\n    ole_objects(i).Delete()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **OLEObjects** member of the **Worksheet** object in Excel represents a collection of all OLE ob&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2284","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=2284"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2284\/revisions"}],"predecessor-version":[{"id":3486,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2284\/revisions\/3486"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}