{"id":2336,"date":"2026-09-28T16:19:48","date_gmt":"2026-09-28T08:19:48","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2336"},"modified":"2026-03-29T09:31:31","modified_gmt":"2026-03-29T09:31:31","slug":"how-to-use-worksheetprevious-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetprevious-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Previous in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>Previous<\/strong> property of a <strong>Worksheet<\/strong> object in Excel&#8217;s object model provides a convenient way to navigate to the preceding worksheet within the workbook, based on the physical tab order. In xlwings, this functionality is exposed through the <code>api<\/code> property, which grants direct access to the underlying Excel object model. This is particularly useful for automating tasks that require sequential processing of sheets or for creating navigation macros.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The <code>Previous<\/code> property returns a <strong>Worksheet<\/strong> object representing the sheet immediately before the active or specified worksheet. If the current sheet is the first one, accessing <code>Previous<\/code> will return <code>None<\/code>. It is a read-only property.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, you access this property via the <code>api<\/code> object. The general syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>previous_sheet = ws.api.Previous<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Where <code>ws<\/code> is an xlwings <code>Sheet<\/code> object (which corresponds to a Worksheet). The returned value is a COM object representing the previous worksheet. To integrate it seamlessly with xlwings, you typically wrap it with <code>xlwings.Sheet<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Consider a workbook with three worksheets named &#8220;Data&#8221;, &#8220;Analysis&#8221;, and &#8220;Summary&#8221; in that order. The following xlwings code demonstrates how to use the <code>Previous<\/code> property.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook\nwb = xw.books.active\n\n# Get the \"Analysis\" sheet\nws_analysis = wb.sheets&#91;'Analysis']\n\n# Access the Previous property via the Excel object model\nprevious_excel_ws = ws_analysis.api.Previous\n\n# Convert the Excel worksheet object to an xlwings Sheet object\nif previous_excel_ws is not None:\n    previous_sheet = xw.Sheet(previous_excel_ws)\n    print(f\"The previous sheet is: {previous_sheet.name}\") # Output: The previous sheet is: Data\nelse:\n    print(\"This is the first sheet.\")\n\n# Example: A practical loop to process sheets backwards from a given point\ncurrent_sheet = wb.sheets&#91;'Summary']\nwhile True:\n    prev_excel_ws = current_sheet.api.Previous\n    if prev_excel_ws is None:\n        break\n    prev_sheet = xw.Sheet(prev_excel_ws)\n    print(f\"Processing: {prev_sheet.name}\")\n    # Perform your operations on prev_sheet here, e.g., read values\n    data_range = prev_sheet.range('A1').expand('table')\n    print(f\"Data range size: {data_range.shape}\")\n    # Move to the previous sheet for the next iteration\n    current_sheet = prev_sheet<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **Previous** property of a **Worksheet** object in Excel&apos;s object model provides a convenient wa&#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-2336","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2336","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=2336"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2336\/revisions"}],"predecessor-version":[{"id":3561,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2336\/revisions\/3561"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}