{"id":2294,"date":"2026-09-07T16:55:28","date_gmt":"2026-09-07T08:55:28","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2294"},"modified":"2026-03-28T12:36:04","modified_gmt":"2026-03-28T12:36:04","slug":"how-to-use-worksheetscenarios-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetscenarios-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Scenarios in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In Excel, a <strong>Scenario<\/strong> is a set of input values (called changing cells) that you can save and later substitute into a worksheet to see different outcomes. The <code>Scenarios<\/code> collection of a <code>Worksheet<\/code> object in Excel&#8217;s object model allows you to manage these saved scenarios. Through xlwings, you can programmatically access, create, modify, and apply these scenarios, enabling powerful what-if analysis automation within your Python scripts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <code>Scenarios<\/code> member provides a way to interact with all scenarios defined on a specific worksheet. You can add new scenarios, retrieve existing ones, change their values, show (apply) a particular scenario, and delete scenarios. This is particularly useful for building financial models, project plans, or any analysis where you need to quickly switch between different sets of assumptions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Key Members<\/strong><br>In xlwings, you access the <code>Scenarios<\/code> collection via the <code>api<\/code> property of a <code>Sheet<\/code> object (which corresponds to a Worksheet). The primary properties and methods include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Accessing the Collection:<\/strong> <code>sheet.api.Scenarios<\/code><\/li>\n\n\n\n<li><strong>Count Property:<\/strong> <code>sheet.api.Scenarios.Count<\/code> returns the number of scenarios on the sheet.<\/li>\n\n\n\n<li><strong>Item Method:<\/strong> <code>sheet.api.Scenarios(Index)<\/code> or <code>sheet.api.Scenarios(Name)<\/code> retrieves a specific <code>Scenario<\/code> object. <code>Index<\/code> can be the scenario&#8217;s number (1-based) or name.<\/li>\n\n\n\n<li><strong>Add Method:<\/strong> Used to create a new scenario.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.api.Scenarios.Add(Name, ChangingCells, Values, Comment, Locked, Hidden)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Name<\/code> (String, Required): The name for the new scenario.<\/li>\n\n\n\n<li><code>ChangingCells<\/code> (Object, Required): An xlwings Range object (e.g., <code>sheet.range(\"B2:B3\")<\/code>), specifying the cells that will change.<\/li>\n\n\n\n<li><code>Values<\/code> (Variant, Optional): An array of values to be entered into the changing cells. If omitted, the current values in the cells are used.<\/li>\n\n\n\n<li><code>Comment<\/code> (String, Optional): A comment describing the scenario (up to 255 characters).<\/li>\n\n\n\n<li><code>Locked<\/code> (Boolean, Optional): <code>True<\/code> to prevent modifications when the sheet is protected.<\/li>\n\n\n\n<li><code>Hidden<\/code> (Boolean, Optional): <code>True<\/code> to hide the scenario when the sheet is protected.<\/li>\n\n\n\n<li>A <code>Scenario<\/code> object itself has key methods like:<\/li>\n\n\n\n<li><code>Show()<\/code>: Applies the scenario&#8217;s values to the worksheet.<\/li>\n\n\n\n<li><code>ChangeScenario(ChangingCells, Values)<\/code>: Modifies the scenario&#8217;s changing cells or values.<\/li>\n\n\n\n<li><code>Delete()<\/code>: Removes the scenario.<\/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>Adding a New Scenario:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"Analysis.xlsx\")\nsheet = wb.sheets&#91;\"Sheet1\"]\n\n# Define changing cells and values\nchanging_cells = sheet.range(\"B2, B4\") # Assumptions for Price and Units\nscenario_values = &#91;29.99, 1200]\n\n# Add a \"Best Case\" scenario\nsheet.api.Scenarios.Add(Name=\"Best Case\",\nChangingCells=changing_cells,\nValues=scenario_values,\nComment=\"Optimistic sales forecast\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Applying (Showing) an Existing Scenario:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Apply the \"Worst Case\" scenario to see its impact\ntry:\n    sheet.api.Scenarios(\"Worst Case\").Show()\n    print(\"Applied 'Worst Case' scenario.\")\nexcept Exception as e:\n    print(f\"Scenario not found: {e}\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Iterating Through and Managing Scenarios:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># List all scenarios and delete a specific one\nscenarios = sheet.api.Scenarios\nprint(f\"Number of scenarios: {scenarios.Count}\")\n\nfor i in range(1, scenarios.Count + 1):\n    scen = scenarios(i)\n    print(f\"{i}: {scen.Name} - {scen.Comment}\")\n\n# Delete the \"Obsolete\" scenario if it exists\nif scenarios.Count > 0:\n    for scen in scenarios:\n        if scen.Name == \"Obsolete\":\n            scen.Delete()\n            print(\"Deleted 'Obsolete' scenario.\")\n            break<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Modifying a Scenario&#8217;s Values:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Update the values for the \"Base Case\" scenario\ntarget_scenario = sheet.api.Scenarios(\"Base Case\")\nnew_changing_cells = sheet.range(\"B2:B3\")\nnew_values = &#91;25.50, 950]\ntarget_scenario.ChangeScenario(ChangingCells=new_changing_cells, Values=new_values)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Excel, a **Scenario** is a set of input values (called changing cells) that you can save and late&#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-2294","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2294","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=2294"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2294\/revisions"}],"predecessor-version":[{"id":3499,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2294\/revisions\/3499"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}