{"id":24,"date":"2026-01-29T18:25:03","date_gmt":"2026-01-29T10:25:03","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=24"},"modified":"2026-01-16T06:41:26","modified_gmt":"2026-01-16T06:41:26","slug":"how-to-set-cell-styles-using-openpyx-background-gradient-fill","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-background-gradient-fill.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Background Gradient Fill"},"content":{"rendered":"<h2>Method<\/h2>\n<p>You can set cell background fill using either gradient fill or pattern fill, using the `GradientFill` and `PatternFill` classes respectively.<\/p>\n<ul>\n<li>Gradient Fill<\/li>\n<\/ul>\n<p>Use the `GradientFill` class to create a gradient fill object and apply it to the cell.<\/p>\n<p>from openpyxl.styles.fills import GradientFill,Stop<\/p>\n<p>GradientFill(type=&#8217;linear&#8217;, degree=0, left=0, right= 0, top=0,bottom=0, stop=())<\/p>\n<p>type:\u00a0 &#8216;linear&#8217; for linear gradient, &#8216;path&#8217; for path gradient.<\/p>\n<p>degree: From 0 to 360, with 0 degrees being horizontal to the right, rotating clockwise.<\/p>\n<p>Stop(&#8216;FF0000&#8217;,0.5)<\/p>\n<p>stop1=(&#8216;FF0000&#8242;,&#8217;00FF00&#8242;,&#8217;0000FF&#8217;)\u00a0\u00a0\u00a0 #Equal intervals<\/p>\n<p>stop2=(Stop(&#8216;FF0000&#8217;,0.0),Stop(&#8216;FF0000&#8217;,0.3),Stop(&#8216;FF0000&#8217;,1.0))\u00a0\u00a0\u00a0 #Unequal intervals<\/p>\n<p>cl_1.fill=GradientFill(type=&#8217;linear&#8217;, stop=stop1)<\/p>\n<p>cl_2.fill=GradientFill(type=&#8217;linear&#8217;, stop=stop2)<\/p>\n<p>cl_3.fill=GradientFill(type=&#8217;linear&#8217;, degree=45,stop=stop1)<\/p>\n<p>cl_4.fill=GradientFill(type=&#8217;path&#8217;, left=0.5, right= 0.5, top=0.5,bottom=0.5, stop=stop1)<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Gradient Fill\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 GradientFill class\nfrom openpyxl.styles import GradientFill\n#Apply gradient color fill to cells\n#Linear gradient: gradient from one side of the cell to the other\n#Path gradient: gradient from the four edges of the cell inward\nws&#91;'B2'].fill=GradientFill(type='linear', \\\n        degree=0, left=0, right=0, top=0, bottom=0, \\\n        stop=&#91;'FF0000','0000FF'])\nws&#91;'E2'].fill=GradientFill(type='linear', \\\n        degree=45, left=0, right=0, top=0, bottom=0, \\\n        stop=&#91;'FF0000','0000FF'])\nws&#91;'G2'].fill = GradientFill(type='path', \\\n        left=0.2, right=0.8, top=0.3, bottom=0.7, \\\n        stop=&#91;'FF0000','0000FF'])\nws.row_dimensions&#91;4].fill=GradientFill(type='linear', \\\n        degree=0, left=0, right=0, top=0, bottom=0, \\\n        stop=&#91;'FF0000','00FF00'])\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\/10.png\" alt=\"Set Cell Styles Using OpenPyX - Background Gradient Fill\" class=\"wp-image-247\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/10.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/10-300x151.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/10-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-24","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/24","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=24"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/24\/revisions\/248"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=24"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=24"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=24"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}