{"id":2377,"date":"2026-10-19T07:04:46","date_gmt":"2026-10-18T23:04:46","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2377"},"modified":"2026-03-29T10:04:06","modified_gmt":"2026-03-29T10:04:06","slug":"how-to-use-rangeclearformats-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeclearformats-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.ClearFormats in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>ClearFormats<\/code> member of the <code>Range<\/code> object in Excel&#8217;s object model is a method used to remove all formatting from a specified range of cells. This includes clearing number formats, font styles (such as bold, italic, color, and size), cell borders, fill colors, alignment settings, and any other applied formatting. It is particularly useful when you need to reset the visual appearance of cells to the default state without affecting the underlying data or formulas, ensuring a clean slate for applying new styles or standardizing reports. In xlwings, this functionality is accessible through the API, allowing for automation of formatting cleanup in Excel workbooks via Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The <code>ClearFormats<\/code> method is called on a <code>Range<\/code> object. The syntax is straightforward, as it does not require any parameters. In xlwings, you typically access a range using the <code>range<\/code> property of a <code>Sheet<\/code> object or directly from the <code>app<\/code> context. The method is applied as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.clear_formats()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>range_object<\/code> refers to an xlwings <code>Range<\/code> instance representing one or more cells in Excel. The method operates in-place and returns <code>None<\/code>. It is equivalent to the VBA method <code>Range.ClearFormats<\/code>, but adapted to Python&#8217;s naming conventions (using snake_case).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Usage:<\/strong><br>Below is a practical example demonstrating how to use <code>ClearFormats<\/code> with xlwings to remove formatting from a specific range. This example assumes you have an Excel workbook open or create one via xlwings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance or start a new one\napp = xw.App(visible=True) # Set visible=False for background operation\nwb = app.books.add() # Create a new workbook; use app.books&#91;'WorkbookName.xlsx'] to open an existing one\nsheet = wb.sheets&#91;'Sheet1']\n\n# Populate some data with formatting for demonstration\nsheet.range('A1').value = 'Sample Data'\nsheet.range('A1').font.bold = True\nsheet.range('A1').font.color = (255, 0, 0) # Red text\nsheet.range('A1:C3').value = &#91;&#91;1, 2, 3], &#91;4, 5, 6], &#91;7, 8, 9]]\nsheet.range('A1:C3').api.Interior.Color = 65535 # Yellow fill via Excel API (optional)\nsheet.range('B2').number_format = '$#,##0.00'\n\n# Clear formatting from a specific range, e.g., cells A1 to C3\nsheet.range('A1:C3').clear_formats()\n\n# Verify by checking that formatting is removed (data remains intact)\nprint(sheet.range('A1:C3').value) # Output: &#91;&#91;'Sample Data', 2, 3], &#91;4, 5, 6], &#91;7, 8, 9]]\nprint(sheet.range('B2').number_format) # Likely returns 'General' or default\n\n# Save and close\nwb.save('ClearedFormats.xlsx')\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `ClearFormats` member of the `Range` object in Excel&apos;s object model is a method used to remove a&#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-2377","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2377","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=2377"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2377\/revisions"}],"predecessor-version":[{"id":3621,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2377\/revisions\/3621"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}