How To Set Cell Styles Using OpenPyX – Alignment

Method

Use the `Alignment` class constructor to create an `Alignment` object, which you can use to set the alignment of data in a cell.

from openpyxl.styles.alignment import Alignment

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)

Where

  • horizontal—Horizontal alignment, which must be one of: “general”“center”“justify”“distributed”“fill”“right”“centerContinuous”“left”
  • vertical—Vertical alignment, which must be one of: “bottom”“distributed”“justify”“center”“top”
  • textRotation—The text rotation angle in degrees. Its value must be one of the following.

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

  • wrapText—Whether text wrapping is allowed (Boolean).
  • shrinkToFit—Whether to shrink the text to fit the cell.
  • indent—The indent level, a floating-point number.
  • relativeIndent—The relative indent level, a floating-point number.
  • justifyLastLine—Whether to justify the last line (Boolean).
  • readingOrder—The reading order, a floating-point number.
  • text_rotation—An alias for **textRotation**, used when the property name is invalid, conflicts with a Python keyword, or is more descriptive.
  • wrap_text—An alias for wrapText
  • shrink_to_fit—An alias for shrinkToFit
  • mergeCell—Merges the cell.

Sample Code

#Cell Style: Set the Alignment

#Import load_workbook function
from openpyxl import Workbook

#Create a new workbook
wb=Workbook()
#Get the active worksheet
ws=wb.active

#Import the Alignment class
from openpyxl.styles import Alignment

#Set the alignment
align1=Alignment(horizontal='center', vertical='top')
align2=Alignment(horizontal='right', vertical='bottom', \
                 text_rotation=30, wrap_text=True, \
                 shrink_to_fit=True, indent=0)
align3=Alignment(horizontal='center', vertical='center', \
                 wrap_text=True, indent=3)

#Apply the first alignment style to cell C2
ws['C2'].alignment=align1
ws['C2'].value='Python123'

#Apply the second alignment style to cell C4
ws['C4'].alignment=align2
ws['C4'].value='Python123'

#Apply the third alignment style to cell C6
ws['C6'].alignment=align3
ws['C6'].value='Python123'

wb.save('test.xlsx')
wb.close()
Set Cell Styles Using OpenPyX - Alignment

Leave a Reply

Your email address will not be published. Required fields are marked *