{"id":26,"date":"2026-01-30T18:06:00","date_gmt":"2026-01-30T10:06:00","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=26"},"modified":"2026-01-16T06:46:45","modified_gmt":"2026-01-16T06:46:45","slug":"how-to-set-cell-styles-using-openpyx-border","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-border.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Border"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Use the `Border` class to create a border object and apply it to a cell.<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">from openpyxl.styles.borders import Border,Side<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Border(left=&lt;openpyxl.styles.borders.Side object&gt; Parameters: style=None, color=None, right=&lt;openpyxl.styles.borders.Side object&gt; Parameters: style=None, color=None, top=&lt;openpyxl.styles.borders.Side object&gt; Parameters: style=None, color=None, bottom= &lt;openpyxl.styles.borders.Side object&gt;<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Parameters: style=None, color=None, diagonal=&lt;openpyxl.styles.borders. Side object&gt; Parameters: style=None, color=None, diagonal_direction=None, vertical=None, horizontal=None, diagonalUp=False, diagonalDown=False, outline= True, start=None, end=None)<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"en-US\">Where<\/span><span lang=\"zh-CN\">\uff1a<\/span><\/p>\n<ul>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\" style=\"background: white;\">left<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">right<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">top<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">bottom<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">diagonal<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">Define the left, right, top, bottom, and diagonal borders, as **Side** objects.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\" style=\"background: white;\">diagonalDown<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">diagonalUp<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">Boolean values, define the direction of the diagonal line. It can be drawn from the top-left to bottom-right, or from the bottom-left to top-right.<\/span><\/li>\n<\/ul>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">A Side object represents a border, which is a line-shaped graphical element. Its main properties include the line style and color. The constructor for this Side object is as follows:<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Side(style=None, color=None, border_style=None)<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"en-US\">Where<\/span><span lang=\"zh-CN\">\uff1a<\/span><\/p>\n<ul>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\" style=\"background: white;\">style<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">The border style, which must be one of: <\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;hair&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;dashed&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;mediumDashDot&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;mediumDashDotDot&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;slantDashDot&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;double&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;thick&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;mediumDashed&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;thin&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;medium&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;dashDotDot&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;dashDot&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;dotted&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3002<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\" style=\"background: white;\">color<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">The color of the border.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\" style=\"background: white;\">border_style<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">An alias for style.<\/span><\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Setting Borders\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 Border class and Side class\nfrom openpyxl.styles import Border, Side\n\n#Set cell borders\n#Use the Border function to create individual borders\nws.cell(row=4, column=4).border = Border(left=Side(border_style='thin', \\\n            color='FF0000'), right=Side(border_style='thin', \\\n            color='FF0000'), top=Side(border_style='double', \\\n            color='FF0000'), bottom=Side(border_style='double', \\\n            color='FF0000'))\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"397\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/12.png\" alt=\"Set Cell Styles Using OpenPyX - Border\" class=\"wp-image-253\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/12.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/12-300x151.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/12-768x387.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\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-26","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/26","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=26"}],"version-history":[{"count":3,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":255,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/26\/revisions\/255"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}