- HD
- 720p
- 540p
- 360p
- 0.50x
- 0.75x
- 1.00x
- 1.25x
- 1.50x
- 1.75x
- 2.00x
We hope you enjoyed this lesson.
Cool lesson, huh? Share it with your friends
About this lesson
We can change the font color and sizing by using the font property.
Exercise files
Download this lesson’s related exercise files.
Cell Font Properties60.3 KB Cell Font Properties - Solution
58.2 KB
Quick reference
Cell Font Properties
We can change the font color and sizing by using the font property.
When to use
Use these techniques whenever you want to change the style of the text in a cell.
Instructions
# Import Font:
from openpyxl.styles import Font
# Define the cell you want to style
cell = ws['A2']
# Style your cell
cell.font = Font(
color = "333333",
size = 18,
bold = False,
italic = False,
underline = 'none',
strike = False
)
Hints & tips
- from openpyxl.styles import Font
- cell.font = Font()
- 00:01 Okay, in this video I want to start to look at the style of ourselves and
- 00:06 we're going to look at the font property.
- 00:08 And the font property allows us to change the you know, the font and
- 00:13 the color and the sizing of text in ourselves.
- 00:16 So I've got the same format that py file that we were working on in the last video.
- 00:20 I deleted some of the stuff we don't need.
- 00:22 But we've got the same, we're loading our pizza.
- 00:24 We're merging all these cells, adding that cell as something, and
- 00:28 saving it as pizza three.
- 00:30 So let's come up to the top here and let's type in from openpyxl.styles,
- 00:34 we want to import font.
- 00:36 And this does just what it sounds like.
- 00:38 It allows us to use this font property with the styles in our spreadsheets.
- 00:43 So let's come down here.
- 00:44 Let's create a variable,called cell and and
- 00:48 let's just define it as whatever is in this cell.
- 00:53 And we know that that is the word something.
- 00:55 So now this variable cell contains the word something.
- 00:58 So now we can use our font property.
- 01:00 So we can go cell dot font and set that equal to Font.
- 01:06 Now this is a capital F and this is a function.
- 01:08 And inside of this function, we can do all different sorts of things.
- 01:11 So the first thing we could do is change the color.
- 01:14 Right?
- 01:14 So let me just type out these and then we'll talk about them individually.
- 01:17 We can change the size.
- 01:23 We can change bold.
- 01:24 We can change metallic We can change underline, and we can change strike.
- 01:33 So, let me come through here and some of these we put actual values and some of
- 01:38 these are true or false so bold for instance, we would put false for not bold,
- 01:42 italic, same thing false for not italic, underline for now let's just put none.
- 01:47 And strike = False.
- 01:52 For size we can pick a font size.
- 01:54 So let's go ahead and just pick 20.
- 01:56 And for color we actually need to use something called hex codes and
- 02:00 hex codes are six digit codes.
- 02:02 That can be used to represent any type of color.
- 02:05 And you likely don't know any hex-code because nobody memorizes them.
- 02:08 So we just have to head over to Google and we can just type in color hex-codes or
- 02:13 something like that.
- 02:14 And you're going to see just hundreds of websites.
- 02:17 So for instance we can just pick this first one here.
- 02:19 And you could see, we've got this little thing here and
- 02:22 all of these color code things look at the same way.
- 02:25 There is usually this slide where you can pick a color.
- 02:27 And let's move this into the blues.
- 02:29 And then we can pick in here exactly what colour blue we want.
- 02:31 We can see it right here.
- 02:33 Over here, it's a lighter thing.
- 02:34 So I'm going to pick right about here.
- 02:36 And you can see the first thing listed with a hashtag a number sign,
- 02:40 that's the hex colour code.
- 02:42 And you can see it's six digits, so 1 2 3 4 5 6 so
- 02:46 we could just go ahead and copy this Highlight it right click copy or
- 02:51 Ctrl C on your keyboard, head back over to our code and just paste this in.
- 02:57 Right click paste or Ctrl V to paste it in.
- 02:59 And this one I'll make this, that sort of blue color.
- 03:02 So we've got a blue color, we've got size of 20, not bold,
- 03:06 not italics, not underlined and not going to put a comment into this.
- 03:10 You probably don't need it, but
- 03:11 it's always good idea to put commas behind everything.
- 03:14 Just in case so alright, let's go ahead and save this.
- 03:16 And let's head over here.
- 03:17 And let's run Python format.pi again.
- 03:20 All right, now we can open up our pizza three file.
- 03:24 And when we do we see now it's that blue color and it's larger 20 font, right?
- 03:30 If we close this and then start to play around with this a little bit.
- 03:33 For instance, we can do bold.
- 03:35 True.
- 03:38 All right.
- 03:39 So let's go ahead and save this and run it.
- 03:42 And now if we open our pizza three dot XLS x file, we see now it's bold.
- 03:48 Same thing with italics we close it.
- 03:53 Instead italics to true It will be italicized now.
- 03:57 Underline, there's several different options.
- 03:59 You can have none for no underline.
- 04:03 You can have single and you can have double.
- 04:08 So if we change this to single and save this and
- 04:12 then run it again and then open our pizza three file.
- 04:17 See it's got one single underline, you'll notice it's also italicized.
- 04:20 So if we close this and make some more changes,
- 04:23 let's change italics back to false.
- 04:27 And let's change this to double.
- 04:29 Now this will be double underline if we run this again You
- 04:34 can see there's two lines underneath.
- 04:38 That's fun.
- 04:40 So it's closed this.
- 04:41 And one more thing we can change.
- 04:43 Let's change this back to none and let's set strike to true.
- 04:46 And this will just put a line straight through our text.
- 04:52 So save it and run it.
- 04:54 Open it again.
- 04:54 And you can see there's a line straight through something.
- 04:57 So those are the ways to change the font properties, the colors, the size,
- 05:01 the bold, italics, underline the strikes.
- 05:03 And remember, you just have to import this from open pi Excel dot styles and
- 05:07 port font.
- 05:08 And then you can use this font function with these different attributes inside and
- 05:12 I put these all on separate lines so they're easier to read.
- 05:16 You could put them all on the same line as well, just by, you know, doing this.
- 05:23 But, you know, that's really kind of hard to read, so with Python,
- 05:27 you can put things on separate lines and it works just as well.
- 05:31 So that's all for this video.
- 05:32 And the next video, we'll look at cell alignment and pattern fills.
Lesson notes are only available for subscribers.