{"id":41,"date":"2026-02-04T18:44:53","date_gmt":"2026-02-04T10:44:53","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=41"},"modified":"2026-01-17T05:21:57","modified_gmt":"2026-01-17T05:21:57","slug":"how-to-select-and-clear-cells-ranges-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-select-and-clear-cells-ranges-using-xlwings\/","title":{"rendered":"How To Select and Clear Cells (Ranges) Using xlwings?"},"content":{"rendered":"<h2>Method<\/h2>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Select<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sht.range(&#8216;A1:B10&#8217;).select()<\/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;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;A1:B10&#8217;).Select()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;A1:B10&#8217;).Activate()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Select non-contiguous cell ranges<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.range(&#8216;A1:A5,C3,E1:E5&#8217;).select()<\/p>\n<p lang=\"en-US\" 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;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;A1:A5,C3,E1:E5&#8217;).Activate()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;A1:A5,C3,E1:E5&#8217;).Select()<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Clear all content from a cell range<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).clear()<\/span><\/p>\n<p lang=\"en-US\" 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;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.api.Range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).Clear()<\/span><\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Clear text content of a cell<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).clear_contents()<\/span><\/p>\n<p lang=\"en-US\" 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;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.api.Range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).ClearContents()<\/span><\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Clear cell comments<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.api.Range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).ClearComments()<\/span><\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Clear cell formatting<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">#sht.api.Range(&#8216;B<\/span><span lang=\"en-US\">2<\/span><span lang=\"zh-CN\">:<\/span><span lang=\"en-US\">D<\/span><span lang=\"zh-CN\">5&#8242;).ClearFormats()<\/span><\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Select and clear cells \n\nimport xlwings as xw  #Import the xlwings package\n\napp=xw.App()\nbk=app.books.open('special.xlsx')\nsht=bk.sheets.active  #Get the active worksheet\n\n#Select\n#xlwings\n#sht.range('A1:B10').select()\n\n#xlwings API\n#sht.api.Range('A1:B10').Select()\n#sht.api.Range('A1:B10').Activate()\n\n#Select non-contiguous cell ranges\n#xlwings\n#sht.range('A1:A5,C3,E1:E5').select()\n\n#xlwings API\n#sht.api.Range('A1:A5,C3,E1:E5').Activate()\n#sht.api.Range('A1:A5,C3,E1:E5').Select()\n\n#Clear all content from a cell range\n#xlwings\n#sht.range('B2:D5').clear()\n\n#xlwings API\nsht.api.Range('B2:D5').Clear()\n\n#Clear text content of a cell\n#xlwings\n#sht.range('B2:D5').clear_contents()\n\n#xlwings API\n#sht.api.Range('B2:D5').ClearContents()\n\n#Clear cell comments\n#xlwings API\n#sht.api.Range('B2:D5').ClearComments()\n\n#Clear cell formatting\n#xlwings API\n#sht.api.Range('B2:D5').ClearFormats()\n\n#bk.close()\n#app.kill()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/30.png\" alt=\"Select and Clear Cells (Ranges) Using xlwings\" class=\"wp-image-308\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/30.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/30-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/30-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/31.png\" alt=\"\" class=\"wp-image-309\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/31.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/31-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/31-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/32.png\" alt=\"\" class=\"wp-image-310\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/32.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/32-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/32-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/33.png\" alt=\"\" class=\"wp-image-311\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/33.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/33-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/33-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/34.png\" alt=\"\" class=\"wp-image-312\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/34.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/34-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/34-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/36.png\" alt=\"\" class=\"wp-image-313\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/36.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/36-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/36-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"883\" height=\"482\" src=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/37.png\" alt=\"\" class=\"wp-image-314\" srcset=\"https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/37.png 883w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/37-300x164.png 300w, https:\/\/xlwings.net\/blog\/wp-content\/uploads\/2026\/02\/37-768x419.png 768w\" sizes=\"auto, (max-width: 883px) 100vw, 883px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Method<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-xlwings-cell-range"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/41","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=41"}],"version-history":[{"count":3,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":315,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions\/315"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}