{"id":2299,"date":"2026-09-10T07:21:58","date_gmt":"2026-09-09T23:21:58","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2299"},"modified":"2026-03-28T12:40:18","modified_gmt":"2026-03-28T12:40:18","slug":"how-to-use-worksheetunprotect-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetunprotect-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Unprotect in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Unprotect<\/code> member of the <code>Worksheet<\/code> object in xlwings is used to remove protection from a worksheet that has been previously protected. This is essential when you need to programmatically modify cells, ranges, or other elements that are locked under protection. Without unprotecting the sheet, attempts to write data or change formatting may fail. In xlwings, this functionality directly mirrors the <code>Unprotect<\/code> method in the Excel Object Model, providing a straightforward way to automate security settings in Excel files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, the <code>Unprotect<\/code> method is called on a <code>Sheet<\/code> object (which corresponds to a worksheet). The basic syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.api.Unprotect(Password)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>sheet<\/code> refers to the xlwings <code>Sheet<\/code> object. The <code>.api<\/code> attribute provides access to the underlying Excel object model, allowing you to use the native <code>Unprotect<\/code> method. The <code>Password<\/code> parameter is optional and specifies the password that was used to protect the worksheet. If the worksheet was protected without a password, you can omit this argument or pass <code>None<\/code>. If an incorrect password is provided when one is required, the method will raise an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Password<\/code> (optional, <code>str<\/code>): A string representing the password. It is case-sensitive. If the sheet is not password-protected, this can be omitted.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Usage<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are practical examples demonstrating how to use the <code>Unprotect<\/code> member in xlwings:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Unprotecting a Worksheet Without a Password<\/strong>: If the worksheet was protected without a password, simply call <code>Unprotect<\/code> without any arguments.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook\nwb = xw.Book('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Unprotect the sheet (no password)\nsheet.api.Unprotect()\n\n# Now you can modify the sheet, e.g., write a value\nsheet.range('A1').value = 'New Data'<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Unprotecting a Worksheet With a Password<\/strong>: When the worksheet is password-protected, provide the password as a string.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\nwb = xw.Book('protected_file.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Unprotect using the password 'mysecret'\nsheet.api.Unprotect('mysecret')\n\n# Perform edits after unprotecting\nsheet.range('B2').value = 100<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Handling Protection in a Workflow<\/strong>: You might check if a sheet is protected before unprotecting it to avoid errors. While xlwings does not have a direct property for protection status, you can use the <code>.api.ProtectContents<\/code> property (returns <code>True<\/code> if protected).<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\nwb = xw.Book('workbook.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Check if the sheet is protected\nif sheet.api.ProtectContents:\n    sheet.api.Unprotect(Password='password123') # Use password if set\n    print(\"Sheet unprotected successfully.\")\nelse:\n    print(\"Sheet is not protected.\")\n\n# Continue with data manipulation\nsheet.range('A1:C10').value = &#91;&#91;1, 2, 3], &#91;4, 5, 6]]<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Unprotecting All Worksheets in a Workbook<\/strong>: To unprotect multiple sheets, iterate through them. This example assumes a common password for all sheets.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\nwb = xw.Book('multi_sheet.xlsx')\npassword = 'commonpass'\n\nfor sheet in wb.sheets:\n    if sheet.api.ProtectContents:\n        sheet.api.Unprotect(password)\n        print(f\"Unprotected: {sheet.name}\")\n\n# Now all sheets are editable\nwb.sheets&#91;0].range('A1').value = 'Updated in all sheets'<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Unprotect` member of the `Worksheet` object in xlwings is used to remove protection from a work&#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-2299","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2299","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=2299"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2299\/revisions"}],"predecessor-version":[{"id":3506,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2299\/revisions\/3506"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}