Locked lesson.
About this lesson
Pattern Fill allows you to change the color and pattern of a cell.
Exercise files
Download this lesson’s related exercise files.
26 - Cell Fill (Pattern Fill).docx57.3 KB 26 - Cell Fill (Pattern Fill) SOLUTION.docx
58 KB
Quick reference
Cell Fill (Pattern Fill)
Pattern Fill allows you to change the color and pattern of a cell.
When to use
Use this whenever you want to change the color and pattern of a cell in a spreadsheet.
Instructions
# Import the PatternFill Library
from openpyxl.styles import PatternFill
# Pick a cell
cell = ws["A3"]
# Change the pattern fill
cell.fill = PatternFill(
'solid',
fgColor = 'bb9c68',
)
Your fill options are:
darkGrid, lightUp, gray125, darkVertical, lightDown, lightVertical,
lightHorizontal, mediumGray, lightGrid, darkGray, darkDown, gray0625,
lightGray, darkUp, darkHorizontal, lightTrellis, darkTrellis, solid
Hints & tips
- from openpyxl.styles import PatternFill
- cell.fill = PatternFill()
- 00:01 Okay, in this video, I want to talk about pattern fill.
- 00:05 And pattern fill is just what it sounds like it's filling up your
- 00:09 cell with some sort of pattern and color.
- 00:12 So the first thing we need to do is head up to the top again and
- 00:16 in our same openpyxl.styles line here, we want to add PatternFill.
- 00:20 And notice the P and the F in pattern and fill are both capitalized.
- 00:25 So okay, come down here we've got our same format.py file that we've been
- 00:28 working with.
- 00:29 So we're going to change the fill for ourselves.
- 00:32 So let's go cell.fill and set that = PatternFill and notice the P and
- 00:36 the F are both capitalized, and inside of here we basically only have two arguments.
- 00:42 The first one is what we want to do and we'll start out with just solid.
- 00:46 And the second one is the fgColor, and that stands for
- 00:49 foreground color, that's basically the color of the pattern that you want to do.
- 00:53 So I'm going to paste in just sort of a dark tan color that I found and
- 00:57 this is our hex code.
- 00:59 And again, you can get this by going to the website,
- 01:02 any website that has a hex color code thing.
- 01:05 And I just picked this sort of beige thing because our cell here is blue and blue and
- 01:10 beige, I don't know kind of go together it doesn't really matter.
- 01:15 I just randomly picked a color, so okay, let's go ahead and save this and
- 01:18 run this guy.
- 01:20 So python format.py, and now we can open our pizza3 file again.
- 01:26 And when we do, we see it's just a solid tan color in our cell here, so
- 01:30 very straightforward.
- 01:32 So this is solid, there are about a dozen other options you could pick.
- 01:36 And instead of just typing these out, I'm going to come back here and
- 01:39 do the thing I talked about in the last video where if you're not quite sure,
- 01:43 just type something.
- 01:44 If we save this and now run it, we're going to get a big error message.
- 01:48 But it's going to then tell us all of the different options, so
- 01:50 you can see here is solid.
- 01:51 We've got a bunch of other ones, dark red, light up gray, dark vertical.
- 01:57 So let's just grab a couple of these and test them out.
- 01:59 So I'm going to copy this.
- 02:01 Let's head over here and paste this in, so let's save this.
- 02:04 And let's run this guy again, open up our pizza3 file.
- 02:10 And so now, we get this sort of different style there, that's kind of interesting.
- 02:16 So okay, let's close this one, let's pick a couple more and
- 02:18 just play around with this, because it's kind of fun, right?
- 02:21 So let's see, I'm going to stick with the dark ones because they're easier to see.
- 02:25 So let's copy this darkVertical and paste this in, let's go ahead and
- 02:29 save this and run it.
- 02:33 And then open our pizza3 file, and you can see now we have vertical dark bars up and
- 02:38 down, that's kind of interesting.
- 02:40 In the same sort of vein, we have, let's see,
- 02:44 that was dark vertical, we can do darkHorizontal right here.
- 02:49 So let's copy this and take a look, darkHorizontal,
- 02:54 I noticed these are lowercase and uppercase, that's kind of interesting.
- 02:59 So just keep that in mind.
- 03:01 So let's run this guy and open up our pizza3 file, and now we get resentful and
- 03:07 these are dark, there's also lightVertical, and light whatever.
- 03:12 Let's see, lightVertical, lightDown, darkVertical, what else do we have?
- 03:16 DarkUp, let's see what that one is.
- 03:18 And if you can't tell them,
- 03:19 I'm just playing here because I like playing around with these things.
- 03:24 So all right, let's go ahead and save this,
- 03:26 oops our pizza3 file will still open.
- 03:34 All right, so let's run this again, open this, and
- 03:37 now we've got the sort of slanted lines, that's cool.
- 03:39 All right, let's try one more, what do we got?
- 03:45 Let's see, mediumGray, lightGrid, darkGray, darkDown,
- 03:49 darkTellis, let's see what that one is.
- 03:52 Let's copy this, paste this in here, save it.
- 03:58 Run this guy again, open up our pizza3 file, and
- 04:01 we get the sort of darkTrellis pattern.
- 04:03 So very cool, a lot of fun,
- 04:05 all kinds of things you can do just be as creative as you want and pretty simple.
- 04:09 So that's all for this video, in the next video, we'll look at gradient fills.
Lesson notes are only available for subscribers.