Locked lesson.
About this lesson
In this lesson, we'll discuss methods to change the alignment of text in a cell.
Exercise files
Download this lesson’s related exercise files.
25 - Cell Alignment.docx60.3 KB 25 - Cell Alignment SOLUTION.docx
57.8 KB
Quick reference
Cell Alignment
We can change where text in a cell is aligned.
When to use
Use these methods whenever you want to change the alignment of text in a cell.
Instructions
# Import the Alignment Library
from openpyxl.styles import Alignment
# Pick a cell to align
cell = ws['A2']
# Align the cell
cell.alignment = Alignment(
horizontal = 'center', # left, right, center
vertical = '', # top, bottom, center
)
Hints & tips
- from openpyxl.styles import Alignment
- cell.alignment = Alignment()
- Horizontal: left, right, center
- Vertical: top, bottom, center
Lesson notes are only available for subscribers.