{"id":2394,"date":"2026-10-27T15:51:34","date_gmt":"2026-10-27T07:51:34","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2394"},"modified":"2026-03-29T10:14:01","modified_gmt":"2026-03-29T10:14:01","slug":"how-to-use-rangediscardchanges-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangediscardchanges-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.DiscardChanges in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>DiscardChanges<\/code> member of the <code>Range<\/code> object in Excel&#8217;s object model is a method used within the context of shared workbooks or co-authoring scenarios. Its primary function is to discard any unsaved changes that have been made to a specific range of cells since the last time the workbook was saved. This is particularly useful when multiple users are editing a shared workbook simultaneously, and you need to revert a particular range to its last saved state without affecting other users&#8217; changes or the entire workbook. In xlwings, which provides a Pythonic way to interact with Excel via its API, this method can be accessed to programmatically manage changes in Excel files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In terms of syntax, the <code>DiscardChanges<\/code> method is called on a <code>Range<\/code> object. The xlwings API closely mirrors the Excel object model, so the method is invoked similarly. The general syntax in xlwings is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>range_object.api.DiscardChanges()<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>range_object<\/code> refers to an xlwings <code>Range<\/code> instance that you have defined, such as through <code>sheet.range('A1:B10')<\/code>. The <code>.api<\/code> attribute in xlwings provides direct access to the underlying Excel object model, allowing you to call native Excel methods like <code>DiscardChanges<\/code>. This method does not take any parameters, as it simply discards all changes within the specified range since the last save. It&#8217;s important to note that <code>DiscardChanges<\/code> is only applicable if the workbook is shared; otherwise, it may not have any effect or could raise an error. Additionally, this method is part of Excel&#8217;s older shared workbook feature and might be less commonly used with modern co-authoring in Excel for the web or newer versions, but it remains available for compatibility.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider a scenario where you have a shared Excel workbook, and you want to discard any unsaved changes in the range A1 to C5 on the active sheet. Using xlwings, you can write the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance or open a workbook\napp = xw.apps.active\nwb = app.books&#91;'SharedWorkbook.xlsx'] # Ensure this is a shared workbook\nsheet = wb.sheets&#91;'Sheet1']\n\n# Define the range where changes should be discarded\ntarget_range = sheet.range('A1:C5')\n\n# Discard any unsaved changes in this range\ntarget_range.api.DiscardChanges()\n\n# Save the workbook to persist the state (if needed)\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this code, <code>target_range.api.DiscardChanges()<\/code> calls the native Excel method on the specified range. It&#8217;s crucial to handle this with care, as discarding changes is irreversible and will remove any edits made to that range since the last save. Before using this method, you might want to check if the workbook is shared by inspecting properties like <code>wb.api.MultiUserEditing<\/code>, but note that xlwings does not have a direct property for this; you would access it via <code>.api<\/code>. For instance, to ensure the workbook is shared, you could add a condition:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if wb.api.MultiUserEditing:\n    target_range.api.DiscardChanges()\nelse:\n    print(\"Workbook is not shared. DiscardChanges may not be applicable.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `DiscardChanges` member of the `Range` object in Excel&apos;s object model is a method used within th&#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-2394","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2394","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=2394"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2394\/revisions"}],"predecessor-version":[{"id":3643,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2394\/revisions\/3643"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}