{"id":29,"date":"2026-02-01T11:23:40","date_gmt":"2026-02-01T03:23:40","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=29"},"modified":"2026-01-16T06:55:57","modified_gmt":"2026-01-16T06:55:57","slug":"how-to-set-cell-styles-using-openpyx-protection","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-protection.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Protection"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">from openpyxl.styles import Protection<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#Set protection<\/p>\n<p style=\"margin: 0in; font-size: 14.0pt;\"><span lang=\"zh-CN\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">ws[&#8216;C3&#8217;].protection=Protection(locked=<\/span><span lang=\"en-US\" style=\"font-family: Calibri;\">True<\/span><span lang=\"zh-CN\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1;\">)<\/span><\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws[&#8216;C3&#8242;]=&#8217;abc123&#8217;<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.protection.sheet=True<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Setting Protection\n\n#Import load_workbook function\nfrom openpyxl import Workbook\n\n#Create a new workbook\nwb=Workbook()\n#Get the active worksheet\nws=wb.active\n\n#Import Protection class\nfrom openpyxl.styles import Protection\n\n#Set protection\nws&#91;'C3'].protection=Protection(locked=False)\nws&#91;'C3']='abc123'\nws.protection.sheet=True\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OpenPyXL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":262,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/29\/revisions\/262"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}