{"id":2269,"date":"2026-08-26T07:43:16","date_gmt":"2026-08-25T23:43:16","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2269"},"modified":"2026-03-28T12:09:19","modified_gmt":"2026-03-28T12:09:19","slug":"how-to-use-worksheetsparent-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetsparent-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheets.Parent in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Parent<\/code> property of the <code>Worksheets<\/code> object in the Excel object model is a fundamental attribute that provides a reference to the immediate containing object. In the context of xlwings, a powerful Python library for Excel automation, accessing this property allows you to navigate the object hierarchy efficiently. Specifically, for a <code>Worksheets<\/code> collection, the <code>Parent<\/code> property returns the <code>Workbook<\/code> object to which the worksheets belong. This is particularly useful when you are working with multiple workbooks or need to perform operations at the workbook level based on a worksheet reference.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function is to return the parent object of the <code>Worksheets<\/code> collection. This enables you to access properties and methods of the parent workbook, such as its name, path, or other worksheets, without needing a separate reference. It simplifies code by allowing chained operations and is essential for writing dynamic and reusable scripts that interact with Excel&#8217;s structure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>In xlwings, the <code>Parent<\/code> property is accessed through the <code>api<\/code> property, which exposes the underlying Excel object model. The syntax is straightforward:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>parent_workbook = worksheets_object.api.Parent<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>worksheets_object<\/code> is an instance of <code>xlwings.main.Worksheets<\/code> (or a similar collection object). The <code>.api<\/code> attribute provides the native Excel VBA object model interface, and <code>.Parent<\/code> is called as a property without parentheses. This returns a COM object representing the parent workbook, which can be further used with xlwings or converted to an xlwings <code>Book<\/code> object for easier manipulation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><br>The <code>Parent<\/code> property does not take any parameters. It is a read-only property that automatically retrieves the containing object based on the Excel object hierarchy.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><br>Below are practical examples demonstrating the use of the <code>Parent<\/code> property in xlwings.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Accessing the Parent Workbook from Worksheets:<\/strong><br>This example shows how to get the parent workbook of the worksheets collection and print its name.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook\nwb = xw.Book('example.xlsx')\n\n# Get the Worksheets collection\nworksheets = wb.sheets\n\n# Access the Parent property via .api\nparent_workbook_com = worksheets.api.Parent\n\n# Convert to xlwings Book object for easier use\nparent_workbook = xw.Book(parent_workbook_com)\n\nprint(f\"Parent workbook name: {parent_workbook.name}\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Using Parent to Navigate and Perform Operations:<\/strong><br>In this scenario, we use the <code>Parent<\/code> property to save the workbook after modifying a worksheet.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Start Excel app and open a workbook\napp = xw.App(visible=False)\nwb = app.books.open('data.xlsx')\n\n# Get a specific worksheet\nsheet = wb.sheets&#91;'Sheet1']\n\n# Get the worksheets collection parent (the workbook) and save it\n# This is useful when you only have a reference to the sheet or worksheets\nparent_wb_com = sheet.api.Parent.Parent # First .Parent gets Worksheets, second gets Workbook\n# Alternatively, directly from the sheet's parent:\nparent_wb_com = sheet.api.Parent\n\n# Save the workbook using the COM object\nparent_wb_com.Save()\n\n# Close\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Dynamic Workbook Reference in a Function:<\/strong><br>This example creates a function that uses the <code>Parent<\/code> property to work with any worksheet object.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\ndef get_workbook_path(worksheet):\n\"\"\"Return the full path of the workbook containing the given worksheet.\"\"\"\nparent_com = worksheet.api.Parent\nparent_wb = xw.Book(parent_com)\nreturn parent_wb.fullname\n\n# Usage\nwb = xw.Book('inventory.xlsx')\nsheet = wb.sheets&#91;0]\npath = get_workbook_path(sheet)\nprint(f\"Workbook located at: {path}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Parent` property of the `Worksheets` object in the Excel object model is a fundamental attribut&#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-2269","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2269","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=2269"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":3465,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2269\/revisions\/3465"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}