{"id":2370,"date":"2026-10-15T15:14:12","date_gmt":"2026-10-15T07:14:12","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2370"},"modified":"2026-03-29T09:59:45","modified_gmt":"2026-03-29T09:59:45","slug":"how-to-use-rangeborderaround-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeborderaround-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.BorderAround in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>BorderAround<\/code> method in the Excel object model is used to apply a border around the entire range of cells. This is particularly useful for visually distinguishing a block of data, such as a summary table or a key dataset, within a worksheet. In xlwings, this method is accessed through the <code>api<\/code> property of a Range object, which provides direct access to the underlying Excel object model. The method allows you to specify the line style, weight, and color of the border.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>The xlwings API call follows the pattern: <code>range.api.BorderAround(LineStyle, Weight, ColorIndex, Color)<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>LineStyle<\/code>: An integer or constant from the <code>XlLineStyle<\/code> enumeration that defines the style of the border line. Common values include:<\/li>\n\n\n\n<li><code>1<\/code> or <code>xlContinuous<\/code>: A continuous solid line.<\/li>\n\n\n\n<li><code>-4115<\/code> or <code>xlDash<\/code>: A dashed line.<\/li>\n\n\n\n<li><code>-4118<\/code> or <code>xlDot<\/code>: A dotted line.<\/li>\n\n\n\n<li><code>4<\/code> or <code>xlDouble<\/code>: A double line.<\/li>\n\n\n\n<li><code>Weight<\/code>: An integer or constant from the <code>XlBorderWeight<\/code> enumeration that specifies the thickness of the border. Typical values are:<\/li>\n\n\n\n<li><code>2<\/code> or <code>xlThin<\/code>: A thin border.<\/li>\n\n\n\n<li><code>-4138<\/code> or <code>xlMedium<\/code>: A medium border.<\/li>\n\n\n\n<li><code>4<\/code> or <code>xlThick<\/code>: A thick border.<\/li>\n\n\n\n<li><code>ColorIndex<\/code>: An optional integer representing a color from the current color palette. Use this or <code>Color<\/code>, but not both. If omitted, the default color (usually black) is used.<\/li>\n\n\n\n<li><code>Color<\/code>: An optional argument to specify the border color as an RGB value. This can be provided as an integer (e.g., <code>0xFF0000<\/code> for red) or using the <code>RGB<\/code> function from the Excel object model via <code>xlwings<\/code>. If both <code>ColorIndex<\/code> and <code>Color<\/code> are omitted, the default color applies.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Here is an xlwings code example that demonstrates the use of <code>BorderAround<\/code>. First, ensure you have xlwings installed and an Excel application running.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance and get the active sheet\napp = xw.apps.active\nwb = app.books.active\nsheet = wb.sheets.active\n\n# Define a range, e.g., A1:D10\ntarget_range = sheet.range('A1:D10')\n\n# Apply a thick, continuous red border around the range\n# Using Color as an RGB integer for red (0xFF0000)\ntarget_range.api.BorderAround(LineStyle=1, Weight=4, Color=0xFF0000)\n\n# Alternatively, use constants from the Excel enumeration for clarity\n# First, import the constants (optional, as you can use numeric values)\nfrom xlwings.constants import LineStyle, BorderWeight\ntarget_range.api.BorderAround(LineStyle=LineStyle.xlContinuous,\nWeight=BorderWeight.xlThick,\nColor=0x0000FF) # Blue border\n\n# Another example with a dashed medium border using default color\ntarget_range.api.BorderAround(LineStyle=-4115, Weight=-4138)\n\n# Save the workbook if needed\nwb.save('output_with_borders.xlsx')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `BorderAround` method in the Excel object model is used to apply a border around the entire rang&#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-2370","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2370","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=2370"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions"}],"predecessor-version":[{"id":3611,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2370\/revisions\/3611"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}