{"id":2340,"date":"2026-09-30T16:17:19","date_gmt":"2026-09-30T08:17:19","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2340"},"modified":"2026-03-29T09:35:27","modified_gmt":"2026-03-29T09:35:27","slug":"how-to-use-worksheetprotection-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetprotection-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Protection in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>Protection<\/strong> member of a <strong>Worksheet<\/strong> object in xlwings provides a way to control and query the protection settings of a worksheet. This is essential for securing data by preventing unauthorized users from modifying cells, formatting, or other elements. Through xlwings, you can access the protection properties to check the current protection status, apply protection with specific options, or unprotect the sheet if needed. It&#8217;s a powerful feature for automating the security aspects of Excel workbooks in Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <strong>Protection<\/strong> object allows you to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Protect a worksheet to restrict editing.<\/li>\n\n\n\n<li>Unprotect a worksheet to allow edits.<\/li>\n\n\n\n<li>Check if a worksheet is currently protected.<\/li>\n\n\n\n<li>Customize protection settings, such as allowing users to select locked cells, format cells, insert rows, etc.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, you access the <strong>Protection<\/strong> member via the <code>api<\/code> property to interact with the underlying Excel object model. The typical syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.protection # This returns the Protection object<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To protect a worksheet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.api.Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To unprotect a worksheet:<\/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\">To check protection status:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sheet.api.ProtectContents # Returns True if the worksheet is protected<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters and Values<\/strong><br>The <code>Protect<\/code> method has multiple optional parameters that control what users can do. Here are key parameters and their meanings:<\/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>Typical Values<\/th><\/tr><\/thead><tbody><tr><td>Password<\/td><td>String<\/td><td>A password to protect the sheet (optional).<\/td><td>Any string, e.g., &#8220;mypass123&#8221;<\/td><\/tr><tr><td>DrawingObjects<\/td><td>Boolean<\/td><td>Protects drawing objects (shapes).<\/td><td>True or False<\/td><\/tr><tr><td>Contents<\/td><td>Boolean<\/td><td>Protects cell contents (locked cells).<\/td><td>True or False<\/td><\/tr><tr><td>UserInterfaceOnly<\/td><td>Boolean<\/td><td>If True, protection applies only via UI, not via code.<\/td><td>True or False<\/td><\/tr><tr><td>AllowFormattingCells<\/td><td>Boolean<\/td><td>Allows formatting of cells.<\/td><td>True or False<\/td><\/tr><tr><td>AllowInsertingRows<\/td><td>Boolean<\/td><td>Allows inserting rows.<\/td><td>True or False<\/td><\/tr><tr><td>AllowSorting<\/td><td>Boolean<\/td><td>Allows sorting.<\/td><td>True or False<\/td><\/tr><tr><td>AllowFiltering<\/td><td>Boolean<\/td><td>Allows filtering.<\/td><td>True or False<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For a full list, refer to the Excel VBA documentation, as xlwings passes these directly to Excel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><br>Here are practical examples using xlwings to work with worksheet protection:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Protecting a worksheet with a password and specific allowances:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to an existing workbook and sheet\nwb = xw.Book('example.xlsx')\nsheet = wb.sheets&#91;'Sheet1']\n\n# Protect the sheet with a password, allowing formatting and sorting\nsheet.api.Protect(Password=\"secret123\", AllowFormattingCells=True, AllowSorting=True)\nprint(\"Worksheet protected.\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Unprotecting a worksheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Unprotect the sheet (if no password, omit the argument)\nsheet.api.Unprotect(\"secret123\")\nprint(\"Worksheet unprotected.\")<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Checking if a worksheet is protected:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Check protection status\nif sheet.api.ProtectContents:\n    print(\"The worksheet is protected.\")\nelse:\n    print(\"The worksheet is not protected.\")<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Applying protection with multiple options:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Protect without a password but allow various actions\nsheet.api.Protect(\nPassword=None,\nDrawingObjects=True,\nContents=True,\nAllowInsertingRows=True,\nAllowFiltering=True,\nUserInterfaceOnly=False\n)\nprint(\"Protection applied with custom settings.\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **Protection** member of a **Worksheet** object in xlwings provides a way to control and query t&#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-2340","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2340","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=2340"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2340\/revisions"}],"predecessor-version":[{"id":3568,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2340\/revisions\/3568"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}