{"id":2327,"date":"2026-09-24T07:38:53","date_gmt":"2026-09-23T23:38:53","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2327"},"modified":"2026-03-28T13:12:34","modified_gmt":"2026-03-28T13:12:34","slug":"how-to-use-worksheetindex-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetindex-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Index in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Index<\/code> member of the <code>Worksheet<\/code> object in xlwings provides a read-only property that returns the index number of the worksheet within its parent workbook&#8217;s <code>Worksheets<\/code> collection. This index is a 1-based integer, meaning the first worksheet in a workbook has an <code>Index<\/code> of 1, the second has an index of 2, and so on. This property is particularly useful when you need to programmatically reference or manipulate worksheets based on their positional order, rather than relying on their names. It can be essential for loops, conditional logic, or when organizing sheets dynamically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function is to retrieve the positional index of a worksheet. This index reflects the sheet&#8217;s order as seen in the Excel application&#8217;s tab bar. It is determined by the sheet&#8217;s position from left to right. If sheets are rearranged, their index values change accordingly. The <code>Index<\/code> property is often used in conjunction with the <code>Worksheets<\/code> collection to access specific sheets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the property is accessed directly from a <code>Worksheet<\/code> object. The syntax is straightforward as it does not accept any parameters.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>worksheet.index<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>worksheet<\/code>: This is an xlwings <code>Sheet<\/code> object representing the target worksheet. It is typically obtained via <code>wb.sheets['SheetName']<\/code> or <code>wb.sheets[0]<\/code>.<\/li>\n\n\n\n<li>The property returns an integer (<code>int<\/code>).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Usage:<\/strong><br>Consider a workbook with three worksheets named &#8220;Data&#8221;, &#8220;Summary&#8221;, and &#8220;Chart&#8221;, in that order from left to right.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook\nwb = xw.Book(\"report.xlsx\")\n\n# Get a worksheet by its name\ndata_sheet = wb.sheets&#91;'Data']\nsummary_sheet = wb.sheets&#91;'Summary']\n\n# Retrieve their indices\nprint(f\"Index of 'Data' sheet: {data_sheet.index}\") # Output: 1\nprint(f\"Index of 'Summary' sheet: {summary_sheet.index}\") # Output: 2\n\n# Example 1: Using index in a loop to perform an action on every other sheet\nfor i in range(1, len(wb.sheets) + 1, 2): # Start at 1, step by 2\n    sheet = wb.sheets&#91;i-1] # xlwings collection is 0-based for indexing\n    print(f\"Processing sheet at workbook index {i}: {sheet.name}\")\n\n# Example 2: Conditionally act based on sheet position\nif summary_sheet.index > data_sheet.index:\n    print(\"The Summary sheet is to the right of the Data sheet.\")\n\n# Example 3: Activating a specific sheet by its known index (e.g., the first sheet)\nwb.sheets&#91;0].activate() # Uses 0-based index for the collection\n# This is equivalent to wb.sheets&#91;wb.sheets&#91;0].index - 1].activate()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Index` member of the `Worksheet` object in xlwings provides a read-only property that returns t&#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-2327","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2327","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=2327"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2327\/revisions"}],"predecessor-version":[{"id":3547,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2327\/revisions\/3547"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}