{"id":28,"date":"2026-01-31T17:11:23","date_gmt":"2026-01-31T09:11:23","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=28"},"modified":"2026-01-16T06:54:32","modified_gmt":"2026-01-16T06:54:32","slug":"how-to-set-cell-styles-using-openpyx-alignment","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-set-cell-styles-using-openpyx-alignment.html","title":{"rendered":"How To Set Cell Styles Using OpenPyX &#8211; Alignment"},"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 `Alignment` class constructor to create an `Alignment` object, which you can use to set the alignment of data in 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.alignment import Alignment<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">Alignment(horizontal=None, vertical=None, textRotation=0, wrapText=None, shrinkToFit=None, indent=0, relativeIndent=0, justifyLastLine=None, readingOrder=0, text_rotation=None, wrap_text=None, shrink_to_fit=None, mergeCell=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\">horizontal<\/span><span lang=\"zh-CN\">\u2014Horizontal alignment, which must be one of: <\/span><span lang=\"en-US\">&#8220;general&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;center&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;justify&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;distributed&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;fill&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;right&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;centerContinuous&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;left&#8221;<\/span><span lang=\"zh-CN\">\u3002<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">vertical<\/span><span lang=\"zh-CN\">\u2014Vertical alignment, which must be one of: <\/span><span lang=\"en-US\">&#8220;bottom&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;distributed&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;justify&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;center&#8221;<\/span><span lang=\"zh-CN\">\u3001<\/span><span lang=\"en-US\">&#8220;top&#8221;<\/span><span lang=\"zh-CN\">\u3002<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">textRotation<\/span><span lang=\"zh-CN\">\u2014The text rotation angle in degrees. Its value must be one of the following.<\/span><\/li>\n<\/ul>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180<\/p>\n<ul>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">wrapText<\/span><span lang=\"zh-CN\">\u2014Whether text wrapping is allowed (Boolean).<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">shrinkToFit<\/span><span lang=\"zh-CN\">\u2014Whether to shrink the text to fit the cell.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">indent<\/span><span lang=\"zh-CN\">\u2014The indent level, a floating-point number.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">relativeIndent<\/span><span lang=\"zh-CN\">\u2014The relative indent level, a floating-point number.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">justifyLastLine<\/span><span lang=\"zh-CN\">\u2014Whether to justify the last line (Boolean).<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">readingOrder<\/span><span lang=\"zh-CN\">\u2014The reading order, a floating-point number.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">text_rotation<\/span><span lang=\"zh-CN\">\u2014An alias for **textRotation**, used when the property name is invalid, conflicts with a Python keyword, or is more descriptive.<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">wrap_text<\/span><span lang=\"zh-CN\">\u2014An alias for wrapText<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">shrink_to_fit<\/span><span lang=\"zh-CN\">\u2014An alias for shrinkToFit<\/span><\/li>\n<li style=\"font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14pt;\"><span lang=\"en-US\">mergeCell<\/span><span lang=\"zh-CN\">\u2014Merges the cell.<\/span><\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Cell Style: Set the Alignment\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 the Alignment class\nfrom openpyxl.styles import Alignment\n\n#Set the alignment\nalign1=Alignment(horizontal='center', vertical='top')\nalign2=Alignment(horizontal='right', vertical='bottom', \\\n                 text_rotation=30, wrap_text=True, \\\n                 shrink_to_fit=True, indent=0)\nalign3=Alignment(horizontal='center', vertical='center', \\\n                 wrap_text=True, indent=3)\n\n#Apply the first alignment style to cell C2\nws&#91;'C2'].alignment=align1\nws&#91;'C2'].value='Python123'\n\n#Apply the second alignment style to cell C4\nws&#91;'C4'].alignment=align2\nws&#91;'C4'].value='Python123'\n\n#Apply the third alignment style to cell C6\nws&#91;'C6'].alignment=align3\nws&#91;'C6'].value='Python123'\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=\"507\" src=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/14.png\" alt=\"Set Cell Styles Using OpenPyX - Alignment\" class=\"wp-image-260\" srcset=\"https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/14.png 787w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/14-300x193.png 300w, https:\/\/openpyxl.com\/blog\/wp-content\/uploads\/2026\/01\/14-768x495.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-28","post","type-post","status-publish","format-standard","hentry","category-openpyxl-cell-range"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/28","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=28"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}