{"id":25,"date":"2026-01-30T11:35:18","date_gmt":"2026-01-30T03:35:18","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=25"},"modified":"2026-01-16T06:43:54","modified_gmt":"2026-01-16T06:43:54","slug":"how-to-set-cell-styles-using-openpyx-background-pattern-fill","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-background-pattern-fill.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Background Pattern Fill"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">You can set cell background fill using either gradient fill or pattern fill, using the `GradientFill` and `PatternFill` classes respectively.<\/p>\n<ul style=\"direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in;\" type=\"disc\">\n<li style=\"margin-top: 4pt; margin-bottom: 4pt; vertical-align: middle;\"><span style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Pattern Fill<\/span><\/li>\n<\/ul>\n<p style=\"margin-left: .375in; margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Use the `PatternFill` class to create a pattern fill object.<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">from openpyxl.styles.fills import PatternFill<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">PatternFill(patternType=None,fgColor=&lt;openpyxl.styles.colors.Color object&gt;<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Parameters: rgb=&#8217;00000000&#8242;, indexed=None, auto=None, theme=None, tint=0.0, type=&#8217;rgb&#8217;, bgColor=&lt;openpyxl. styles.colors.Color object&gt;<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Parameters: rgb=&#8217;00000000&#8242;, indexed=None, auto=None, theme=None, tint=0.0, type=&#8217;rgb&#8217;, fill_type=None, start_color=None, end_color= None)<\/p>\n<p style=\"margin-left: .375in; 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<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"en-US\" style=\"background: white;\">patternType<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">fill_type<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">The pattern fill type, its value must be one of the following: <\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkDown&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;gray0625&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;mediumGray&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkHorizontal&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightVertical&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkGrid&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightGray&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkTrellis&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkVertical&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightGrid&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;solid&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightDown&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightUp&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkUp&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;darkGray&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightTrellis&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;lightHorizontal&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">&#8220;gray125&#8221;<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">None<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3002<\/span><\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"en-US\" style=\"background: white;\">fgColor<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">start_color<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">Foreground color, the value of fgColor must be a Color object.<\/span><\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"en-US\" style=\"background: white;\">bgColor<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u3001<\/span><span lang=\"en-US\" style=\"background: white;\">end_color<\/span><span lang=\"zh-CN\" style=\"background: white;\">\u2014<\/span><span lang=\"zh-CN\">Background color, the value of bgColor must be a Color object.<\/span><\/p>\n<p style=\"margin: 0in; margin-left: .375in; line-height: 15pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">When **fill_type** is set to None, no fill will be applied.<\/p>\n<p style=\"margin: 0in; margin-left: .375in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">When **fill_type** is set to &#8216;Solid&#8217;, it will be a solid color fill.<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Pattern 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 PatternFill class\nfrom openpyxl.styles import PatternFill\nfrom openpyxl.styles.colors import Color\n\n#Pattern fill\nws&#91;'B2'].fill=PatternFill(fill_type=None, \\\n            start_color='FFFF00', end_color='000000')\nws&#91;'C2'].fill=PatternFill(fill_type='solid', \\\n            start_color='00FF00')\nws&#91;'E2'].fill=PatternFill(fill_type='lightGrid', \\\n            start_color='FFFF00', end_color='000000')\nfill=PatternFill(fill_type='lightTrellis', \\\n            fgColor=Color(rgb='00FF00'), bgColor=Color(rgb='0000FF'))\nws.column_dimensions&#91;'B'].fill=fill\nfill=PatternFill(fill_type='lightGray', \\\n            fgColor=Color(rgb='FFFF00'), bgColor=Color(rgb='0000FF'))\nws.row_dimensions&#91;4].fill = fill\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\/11.png\" alt=\"Set Cell Styles Using OpenPyX - Background Pattern Fill\" class=\"wp-image-250\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/11.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/11-300x151.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/11-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-25","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/25","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=25"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/25\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}