{"id":2291,"date":"2026-09-06T07:01:23","date_gmt":"2026-09-05T23:01:23","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2291"},"modified":"2026-03-28T12:33:15","modified_gmt":"2026-03-28T12:33:15","slug":"how-to-use-worksheetprotect-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetprotect-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Protect in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>protect<\/code> method of the <code>Worksheet<\/code> object in xlwings provides a way to secure a worksheet by preventing unauthorized changes. This is particularly useful when you want to share a workbook but restrict editing of specific cells, formulas, or structural elements. By protecting a worksheet, you can allow certain actions, such as selecting cells, while blocking others, like modifying locked cells. In xlwings, this method wraps the corresponding functionality in the Excel object model, offering a programmatic approach to worksheet protection directly from Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The syntax for the <code>protect<\/code> method in xlwings is as follows:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>worksheet.api.Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>worksheet<\/code> is an xlwings <code>Sheet<\/code> object representing the target worksheet. The parameters correspond to those in the Excel VBA <code>Protect<\/code> method, with most being optional boolean values that default to <code>True<\/code> or <code>False<\/code> depending on the action. Key parameters include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Password<\/strong>: A string to set a password for unprotecting the sheet (optional; if omitted, no password is set).<\/li>\n\n\n\n<li><strong>Contents<\/strong>: If <code>True<\/code> (default), protects the contents (locked cells) of the worksheet.<\/li>\n\n\n\n<li><strong>UserInterfaceOnly<\/strong>: If <code>True<\/code>, protection applies only to the UI, allowing macros to make changes via code; defaults to <code>False<\/code>.<\/li>\n\n\n\n<li><strong>AllowFormattingCells<\/strong>, <strong>AllowFormattingColumns<\/strong>, etc.: These boolean parameters control specific user permissions, such as allowing cell formatting or inserting rows; most default to <code>False<\/code> when the sheet is protected.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For example, to protect a worksheet with a password while allowing users to format cells and sort data, you can set <code>AllowFormattingCells<\/code> and <code>AllowSorting<\/code> to <code>True<\/code>. Note that in xlwings, you access this via the <code>.api<\/code> property to call the underlying Excel object model method, as xlwings does not have a native wrapper for all protection options in its high-level API.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below are xlwings API code examples demonstrating the use of the <code>protect<\/code> method:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Basic protection without a password<\/strong>: This protects the worksheet with default settings, preventing edits to locked cells.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\nws.api.Protect()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Protection with a password and specific allowances<\/strong>: Here, a password &#8220;mypass123&#8221; is set, and users are permitted to format cells and insert hyperlinks, while other actions are restricted.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.Protect(Password='mypass123', AllowFormattingCells=True, AllowInsertingHyperlinks=True)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>UI-only protection for macro flexibility<\/strong>: This protects the worksheet in the user interface but allows VBA or xlwings macros to modify it programmatically, without a password.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.Protect(UserInterfaceOnly=True)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Disabling protection<\/strong>: To unprotect a worksheet, use the <code>Unprotect<\/code> method. If a password was set, provide it as an argument.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.api.Unprotect('mypass123') # If password was used\nws.api.Unprotect() # If no password was set<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `protect` method of the `Worksheet` object in xlwings provides a way to secure a worksheet by pr&#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-2291","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2291","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=2291"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":3494,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2291\/revisions\/3494"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}