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
- 00:04 Okay, in this video I want to talk about alignment.
- 00:06 And by alignment I mean where in the cell the text is located, along the right side,
- 00:11 the left side up, down, middle center, those type of things.
- 00:15 So let's hit up here to the top and from the same openpyxll.styles.
- 00:19 We also want to import alignment.
- 00:23 And this will allow us to align things however we want.
- 00:26 So let's come down here and now we can set our
- 00:30 cell.alignment and set that equal to alignment.
- 00:35 And again, this is a function and
- 00:38 this one basically only has two arguments, horizontal, and vertical.
- 00:47 All right, so here are the main options you have for horizontal.
- 00:51 You have left, you have right, you have center and you have a few more that
- 00:56 we're not really going to look at because they're not very useful but
- 01:01 I mentioned them you have justify and you have a centerContinuous.
- 01:06 You also have fill and general.
- 01:09 Now these guys here at the end, they're not very useful.
- 01:13 And they're only using very,
- 01:15 very specific cases that you probably won't ever deal with.
- 01:18 So we're going to look at left, right, center and justify.
- 01:21 So as far as vertical, your options are top,
- 01:26 bottom, center, distributed and justify.
- 01:32 And again, these last two aren't very common or used very often.
- 01:36 So mostly you're looking at top, bottom and center.
- 01:38 So let's take a look at these and let's start with horizontal and
- 01:42 horizontal is left to right like along the horizon.
- 01:45 Vertical is up and down.
- 01:46 And remember, with our A2 cell, it's really big.
- 01:49 So we have a lot of room for different types of alignment.
- 01:52 So let's start out by giving this a horizontal alignment of right and
- 01:57 a vertical alignment of bottom.
- 01:59 It was already on the bottom, just by default, and
- 02:02 we'll just leave this as it is.
- 02:04 So let's go ahead and save this.
- 02:05 And let's head back over here and run our file, again.
- 02:08 And then let's open up our pizza3.xlsx, and
- 02:12 we can see now it's vertically aligned,
- 02:15 this something is over on the far right side, because we put it on the right side.
- 02:21 Now let's get rid of this strike, and let's also change this font size to 12 so
- 02:25 it's smaller, so we could more easily see it move around.
- 02:29 So we have right, we can also go Left.
- 02:32 If we save this and run it, and then open our pizza3.xlsx file,
- 02:38 now it's on the left, right?
- 02:41 Let's close this.
- 02:42 And then finally we can do center.
- 02:46 Let's go ahead and save this.
- 02:48 Run it, and open our file again.
- 02:52 And as you would expect, it's now in the center.
- 02:55 And now notice it's also here in the bottom, right?
- 02:58 So we can change the vertical alignment up and down to whatever we want.
- 03:02 So let's go ahead and close this.
- 03:05 And like I mentioned, there are these other things too.
- 03:06 You can play with them if you want, but they're not really useful.
- 03:09 And you won't hardly even notice them if you try and use them.
- 03:12 So, all right, now we're doing bottom for vertical alignment.
- 03:15 Now let's change this to top, save this, run it.
- 03:20 Open up our pizza3 file, and now something is up here at the top of the cell.
- 03:26 Very cool, so if we close this, and I change this to for
- 03:30 instance center, and go ahead and save this and run it again.
- 03:35 And let's open this guy one more time.
- 03:38 And now it's in the center, vertically and also horizontally.
- 03:43 So very, very cool.
- 03:43 So if we close this for instance, and if we just pick another one of these that I
- 03:48 mentioned, for instance, this distributed.
- 03:51 So if we pick, distributed, I think I misspelled it over here.
- 03:57 There we go.
- 03:58 Save this and run it.
- 04:02 And then open our file again.
- 04:06 You can see it's still in the middle distributed just means basically centered.
- 04:10 Now one interesting thing here is if you can't memorize these things, and
- 04:15 you probably won't, you can just type in anything, just t.
- 04:18 If we save this now and run it, we're going to get an error message and
- 04:21 it's going to say, hey, the value must be either center, top, bottom,
- 04:24 justify or distributed.
- 04:26 So it kind of tells you exactly what your options are.
- 04:29 That's a good way to sort of if you forget what your options are for these things.
- 04:34 Just type in anything and
- 04:35 see if an error message will tell you what your options are.
- 04:38 But these are the main ones left, right and center, top, bottom and center.
- 04:42 And if you think about a cell, it sort of makes sense.
- 04:44 The cell vertically has a top and a bottom and a center.
- 04:48 Horizontally it has a left and a right and a center.
- 04:50 And that's pretty straightforward.
- 04:52 So that's all for this video.
- 04:52 In the next video we'll look at filling cell patterns.
Lesson notes are only available for subscribers.