Locked lesson.
About this lesson
We can easily change the gradient color of a cell in a spreadsheet.
Exercise files
Download this lesson’s related exercise files.
27 - Cell Fill (Gradient Fill).docx57.2 KB 27 - Cell Fill (Gradient Fill) SOLUTION.docx
56 KB
Quick reference
Cell Fill (Gradient Fill)
We can easily change the gradient color of a cell in a spreadsheet.
When to use
Use this whenever you want to add a gradient style to a spreadsheet cell.
Instructions
# Import the GradientFill library
from openpyxl.styles import GradientFill
# Pick a cell to change:
cell = ws['E15']
# Change the Gradient:
cell.fill = GradientFill(
stop = ("startcolor", "stopcolor"),
)
Note: "startcolor" and "stopcolor" in the code above should be replaced with hex color codes, e.g. 000000 and ffffff for black and white.
Hints & tips
- from openpyxl.styles import GradientFill
- cell.fill = GradientFill(stop = ("startcolor", "stopcolor"),)
- 00:04 Okay, in the last video, we looked at pattern fills, in this video,
- 00:06 I want to look at gradient fills.
- 00:06 And gradient fills just allow you to change the color of yourself but in
- 00:11 a gradient using two different colors, so from white to black, you know what I mean?
- 00:16 It's sort of slides from one color into the next.
- 00:21 Isn't it a nice little effect?
- 00:23 So let's come up to the top here and
- 00:26 from our same openpyxl.styles line here we can import GradientFill.
- 00:32 And notice the G and the F are both capitalized in GradientFill.
- 00:36 So okay, let's come down here in our same file here, let's go cell.fill again.
- 00:42 Notice we had cell.fill for our pattern fill as well, but
- 00:45 we do the same thing for Gradient.Fill.
- 00:48 And we set this = to GradientFill.
- 00:53 And this one's a little bit different.
- 00:55 Inside of here, we pass something called stop= and
- 00:58 then another sort of tuple looking thing.
- 01:03 And you can put a comma there if you want, but you don't really need to.
- 01:06 Now inside of here, we put two different colors.
- 01:09 So this is where you want to start and stop.
- 01:11 So, let's head over to our website really quickly.
- 01:13 And this is just our hex color picker.
- 01:16 And again, you can use any hex color picker that you want.
- 01:19 So I'm going to drag this over all the way up to the top to as white as can be and
- 01:25 that is FFFFFF, six Fs.
- 01:27 So I'm going to copy this and let's head back over here and
- 01:30 we want to start at white, for instance, and we want to go to, let's say black.
- 01:35 So I'm going to drag this all the way down here and that's 000000.
- 01:39 So I'll copy this and bring this back.
- 01:42 And notice these are 0s like the number 0, not capital O, right?
- 01:47 So definitely 0s.
- 01:49 So okay, let's go ahead and save this and let's run this.
- 01:51 So Python format.py and then let's go ahead and open up our pizza three file.
- 02:00 And you see now we get this cool gradient.
- 02:02 So it's going from white to black and a very, very interesting.
- 02:07 We can do the opposite if we want, we could change these colors around,
- 02:11 so we could go 000000, and then change these to ffffff.
- 02:15 We save this and run this guy again.
- 02:20 And then open up our pizza3.xlsx file.
- 02:23 Boom, now it starts out black over here and goes to white and
- 02:26 that sort of gradient sort of looking fill.
- 02:29 So very, very cool.
- 02:30 So, play around with this, if this is your thing, if you
- 02:32 want your thing to look like this, you could do all kinds of different things.
- 02:36 So let's change this to, let's go to a more sort of tan thing and
- 02:41 we can pick different colors of tans.
- 02:44 So from lighter tan, I mean, obviously I'm not a designer, so take my word for this.
- 02:50 But it just showing you some things you
- 02:53 could do to a sort of a darker tan down here, copy this.
- 02:59 Bring this over, it doesn't have to just be white and black,
- 03:02 it can be any colors that you want.
- 03:03 That's kind of fun.
- 03:04 So, head back over here on this guy again.
- 03:08 And if we open up our pizza3 file again, we get this sort of light tan to dark tan,
- 03:13 looks kind of nice, but whatever you want really easy to do with the gradient fill.
- 03:19 So that's all for this video.
- 03:20 In the next video, we'll look at changing borders.
Lesson notes are only available for subscribers.