{"id":2420,"date":"2026-11-09T16:35:43","date_gmt":"2026-11-09T08:35:43","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2420"},"modified":"2026-03-29T10:35:03","modified_gmt":"2026-03-29T10:35:03","slug":"how-to-use-rangereplace-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangereplace-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Replace in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Replace<\/code> method of the <code>Range<\/code> object in xlwings is a powerful tool for performing text substitutions within a specified range of cells in an Excel worksheet. It functions similarly to the &#8220;Find and Replace&#8221; feature in the Excel user interface but is fully programmable, allowing for automation of data cleaning, formatting corrections, or bulk updates. This method searches for a specific string or pattern and replaces it with another string across all cells in the target range, offering control over case sensitivity, whole cell matching, and search direction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.replace(what, replacement, lookat, search_order, match_case, match_byte, search_format, replace_format)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>what<\/code> (str): The string to search for. This is the required primary argument.<\/li>\n\n\n\n<li><code>replacement<\/code> (str): The string that will replace the found text.<\/li>\n\n\n\n<li><code>lookat<\/code> (int or xlwings.constants): Optional. Specifies whether to match the entire cell content or part of it. Use <code>xlwings.constants.LookAt.xlWhole<\/code> (or <code>1<\/code>) for entire cell, and <code>xlwings.constants.LookAt.xlPart<\/code> (or <code>2<\/code>) for partial matches. Default is <code>xlPart<\/code>.<\/li>\n\n\n\n<li><code>search_order<\/code> (int or xlwings.constants): Optional. Determines the search order: by rows (<code>xlwings.constants.SearchOrder.xlByRows<\/code> or <code>1<\/code>) or by columns (<code>xlwings.constants.SearchOrder.xlByColumns<\/code> or <code>2<\/code>). Default is <code>xlByRows<\/code>.<\/li>\n\n\n\n<li><code>match_case<\/code> (bool): Optional. If <code>True<\/code>, the search is case-sensitive. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>match_byte<\/code> (bool): Optional. Primarily for double-byte character set languages. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>search_format<\/code> (bool): Optional. If <code>True<\/code>, the search includes cell formatting criteria. Requires setting formats via the <code>Application<\/code> object&#8217;s <code>ReplaceFormat<\/code> in advanced scenarios. Default is <code>False<\/code>.<\/li>\n\n\n\n<li><code>replace_format<\/code> (bool): Optional. If <code>True<\/code>, the replacement applies formatting specified via <code>ReplaceFormat<\/code>. Default is <code>False<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Basic Text Replacement:<\/strong><br>Replace all instances of &#8220;old&#8221; with &#8220;new&#8221; in the range A1:C10, ignoring case.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"workbook.xlsx\")\nws = wb.sheets&#91;\"Sheet1\"]\nws.range(\"A1:C10\").replace(what=\"old\", replacement=\"new\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Case-Sensitive, Whole Cell Match:<\/strong><br>Replace only cells that exactly contain &#8220;Error&#8221; (case-sensitive) with &#8220;OK&#8221;.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from xlwings.constants import LookAt\nws.range(\"D1:D100\").replace(what=\"Error\", replacement=\"OK\", lookat=LookAt.xlWhole, match_case=True)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Replacing Numbers and Using Search Order:<\/strong><br>Replace &#8220;2022&#8221; with &#8220;2023&#8221; in a range, searching by columns.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from xlwings.constants import SearchOrder\nws.range(\"F1:J50\").replace(what=\"2022\", replacement=\"2023\", search_order=SearchOrder.xlByColumns)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Advanced Use with Formats (Conceptual):<\/strong><br>While direct format replacement is more complex and less commonly used in xlwings, it can be initiated by setting <code>search_format<\/code> or <code>replace_format<\/code> to <code>True<\/code> after defining format objects via the Excel application. This typically involves the <code>api<\/code> property for lower-level VBA-like calls.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Replace` method of the `Range` object in xlwings is a powerful tool for performing text substit&#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-2420","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2420","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=2420"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2420\/revisions"}],"predecessor-version":[{"id":3676,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2420\/revisions\/3676"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}