{"id":2245,"date":"2026-08-14T07:49:53","date_gmt":"2026-08-13T23:49:53","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2245"},"modified":"2026-03-28T11:52:13","modified_gmt":"2026-03-28T11:52:13","slug":"how-to-use-workbookclose-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-workbookclose-in-the-xlwings-api-way\/","title":{"rendered":"How to use Workbook.Close in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Close<\/code> member of the <code>Workbook<\/code> object in the xlwings API is used to close a specific Excel workbook. This action is essential for managing system resources and ensuring that changes are saved or discarded as intended. When you close a workbook, you can control whether to save any unsaved changes, specify a file path for saving, or even bypass alerts that might appear during the closing process. This functionality is particularly useful in automation scripts where multiple workbooks are processed sequentially, as it helps prevent memory leaks and keeps the Excel application running smoothly without unnecessary open files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the <code>Close<\/code> method is called on a <code>Book<\/code> object (which represents a workbook). The basic syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wb.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, the method supports optional parameters to customize its behavior:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>save_changes<\/code>: A boolean value that determines whether to save changes before closing. If <code>True<\/code>, the workbook is saved; if <code>False<\/code>, changes are discarded. If omitted, Excel may prompt the user based on the workbook&#8217;s state.<\/li>\n\n\n\n<li><code>route_workbook<\/code>: This parameter is less commonly used in modern Excel versions and is typically set to <code>False<\/code>. It relates to routing workbooks in older workflows.<br>The method does not return any value.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters in Detail:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Parameter<\/th><th>Type<\/th><th>Description<\/th><th>Default Value<\/th><\/tr><\/thead><tbody><tr><td><code>save_changes<\/code><\/td><td>bool<\/td><td>If <code>True<\/code>, saves the workbook before closing. If <code>False<\/code>, discards changes. If not provided, Excel may show a prompt.<\/td><td>None (Excel decides)<\/td><\/tr><tr><td><code>route_workbook<\/code><\/td><td>bool<\/td><td>Used for routing in older Excel versions; generally set to <code>False<\/code>.<\/td><td><code>False<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><br>Here are practical examples of using the <code>Close<\/code> member in xlwings:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Basic Close Without Saving:<\/strong><br>This example opens a workbook and closes it immediately without saving, which is useful for read-only operations.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Open an existing workbook\nwb = xw.Book('example.xlsx')\n# Perform some operations (e.g., read data)\ndata = wb.sheets&#91;'Sheet1'].range('A1').value\n# Close the workbook without saving changes\nwb.close(save_changes=False)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Close and Save Changes:<\/strong><br>In this case, changes made to the workbook are saved automatically upon closing, streamlining the workflow.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('report.xlsx')\n# Modify the workbook (e.g., update a cell)\nwb.sheets&#91;0].range('B2').value = 'Updated Data'\n# Close and save the changes\nwb.close(save_changes=True)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Close Multiple Workbooks in a Loop:<\/strong><br>This example demonstrates closing several workbooks in sequence, which is common in batch processing scripts.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nfile_paths = &#91;'data1.xlsx', 'data2.xlsx', 'data3.xlsx']\nfor path in file_paths:\n    wb = xw.Book(path)\n    # Process each workbook (e.g., aggregate data)\n    print(f\"Processed {path}\")\n# Close each workbook after processing, saving changes\nwb.close(save_changes=True)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Handling Prompts with Close:<\/strong><br>If you want to avoid Excel prompts when closing, ensure to set <code>save_changes<\/code> explicitly. Otherwise, Excel might interrupt automation with a dialog box.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('temp.xlsx')\nwb.sheets&#91;0].range('A1').value = 'Test'\n# Close and let Excel handle saving (may prompt if unsaved changes exist)\nwb.close() # No save_changes specified; use with caution in automation<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Close` member of the `Workbook` object in the xlwings API is used to close a specific Excel wor&#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-2245","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2245","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=2245"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2245\/revisions"}],"predecessor-version":[{"id":3431,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2245\/revisions\/3431"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}