{"id":27,"date":"2026-01-31T11:28:06","date_gmt":"2026-01-31T03:28:06","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=27"},"modified":"2026-01-16T06:51:01","modified_gmt":"2026-01-16T06:51:01","slug":"how-to-set-cell-styles-using-openpyx-number-format","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-number-format.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Number Format"},"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 `numbers` module and the `number_format` property of the cell object to set number formats.<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; from openpyxl.styles import numbers<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span lang=\"zh-CN\">There are two ways to set the number format:<\/span><\/p>\n<h3>1. Using the built-in constants of OpenPyXl:<\/h3>\n<blockquote>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt; ws[&#8220;D2&#8221;].number_format=numbers.FORMAT_GENERAL<\/p>\n<\/blockquote>\n<h3 style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">2. Using a string representation of the number format:<\/h3>\n<blockquote>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt; ws[&#8220;D6&#8243;].number_format=&#8221;yy-mm-dd&#8221;<\/p>\n<\/blockquote>\n<blockquote>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt; ws[&#8220;D8&#8243;].number_format=&#8221;d-mmm-yy&#8221;<\/p>\n<\/blockquote>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">To display numbers in scientific notation:<\/p>\n<blockquote>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt; ws[&#8220;D4&#8221;].number_format = &#8216;0.00E+00&#8217;<\/p>\n<\/blockquote>\n<p lang=\"en-US\" style=\"margin: 0in; line-height: 10pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt; color: black;\">\u00a0<\/p>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">The available format constants and strings in OpenPyXl are as follows:<\/p>\n<ul>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_GENERAL=&#8221;General&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_TEXT=&#8221;@&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_NUMBER=&#8221;0&#8243;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_NUMBER_00=&#8221;0.00&#8243;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_NUMBER_COMMA_SEPARATED1=&#8221;#,##0.00&#8243;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_NUMBER_COMMA_SEPARATED2=&#8221;#,##0.00_-&#8220;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_PERCENTAGE=&#8221;0%&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_PERCENTAGE_00=&#8221;0.00%&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_DATE_YYYYMMDD2=&#8221;yyyy-mm-dd&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_DATE_YYMMDD=&#8221;yy-mm-dd&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_DATE_DDMMYY=&#8221;dd\/mm\/yy&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span style=\"background: white;\">FORMAT_DATE_DMYSLASH=&#8221;d\/m\/y&#8221;<\/span><\/li>\n<li lang=\"en-US\" style=\"font-family: 'Microsoft YaHei'; font-size: 14pt;\"><span style=\"background: white;\">\u2026\u2026<\/span><\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Setting Number Formats\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 numbers class\nfrom openpyxl.styles import numbers\nimport datetime\n\n#Set the number format\nws&#91;'D2']='Test123'\nws&#91;'D6']=datetime.datetime.today()\nws&#91;'D8']=datetime.datetime.today()\nws&#91;'D4']=123.456789\nws&#91;'D2'].number_format=numbers.FORMAT_GENERAL\nws&#91;'D6'].number_format='yy-mm-dd'\nws&#91;'D8'].number_format='d-mmm-yy'\nws&#91;'D4'].number_format='0.00E+00'\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\/13.png\" alt=\"Set Cell Styles Using OpenPyX - Number Format\" class=\"wp-image-257\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/13.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/13-300x151.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/13-768x387.png 768w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\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-27","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/27","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=27"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions"}],"predecessor-version":[{"id":258,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/27\/revisions\/258"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=27"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=27"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=27"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}