Locked lesson.
About this lesson
In this lesson, we'll discuss how to change the border style and thickness of a spreadsheet cell.
Exercise files
Download this lesson’s related exercise files.
28 - Borders and Sides.docx57.3 KB 28 - Borders and Sides SOLUTION.docx
55.9 KB
Quick reference
Borders and Sides
Changing Borders is often important in a spreadsheet.
When to use
Use this whenever you want to change the border style and thickness of a spreadsheet cell.
Instructions
# Import libraries
from openpyxl.styles import Side, Border
# pick a cell
cell = ws['B27]
# Define the Side and Thickness
bd = Side(style="thick", color='000000')
# Change Border
cell.border = Border(
left = bd,
right = bd,
top = bd,
bottom = bd
)
Style options include:
hair, dashDotDot, mediumDashDot, medium,
thick, dashDot, slantDashDot, double, mediumDashed,
dashed, mediumDashDotDot, thin, dotted
Hints & tips
- from openpyxl.styles import Side, Border
- bd = Side(style="thick", color='000000')
- cell.border = Border(left = bd, right = bd, top = bd, bottom = bd)
- 00:04 Okay, in this video, we're going to change the border of a cell.
- 00:07 So you may have a situation where you want the bottom of your cell to
- 00:12 have a bar to sort of separate data, or
- 00:15 you may want to put the entire cell in a box, or on either side.
- 00:20 How do we do that?
- 00:20 Well, that's what we're going to look at in this video.
- 00:22 So, we actually need two things in order to do this.
- 00:25 Let's come to the top here, and from our same openpyxl.styles line here,
- 00:31 we want to also import Side and Border.
- 00:35 And they're both capitalized, the S on Side and the B on Border.
- 00:37 So let's come down here.
- 00:40 So when you want to change a border, it's basically a two-step process.
- 00:42 You have to define the side and sort of the thickness of the line you want to
- 00:46 create, and then you change the border to that thing, right?
- 00:49 So the first thing we do is create a variable, and let's just call it bd,
- 00:53 short for Border, and this is going to equal a Side.
- 00:56 And we want to give this a style of thick, and we'll talk about that in a second.
- 01:02 And we want to give it a color of any color you want,
- 01:05 let's just do black, we know that 000000.
- 01:08 So okay, we've now defined the side, right?
- 01:12 It's going to be thick, and it's going to be black.
- 01:14 And like I said, we'll talk about these styles in a second.
- 01:17 Now we need to just change the actual border.
- 01:20 And before we do that, let's create a different cell.
- 01:22 Let's go dif_cell, and let's set this equal to ws and
- 01:29 then let's call this B8, okay?
- 01:33 So now we want to change our dif_cell, and we want to change the border.
- 01:40 So dif_cell.border, and we set that equal to a Border, and this is a function.
- 01:47 And inside of here, we can define the sides that we want to deal with.
- 01:52 So we could go left equals, and we want,
- 01:54 what do we want to do on the left-hand side?
- 01:56 We want to add this bd that we just created, right?
- 01:59 So left is bd.
- 02:00 Now notice this is a lowercase l, I know it kind of looks capital, but
- 02:03 that's definitely lowercase sublime just makes it look like that.
- 02:06 So then we can also go top=bd,
- 02:10 we can go right=bd, and we can go bottom=bd.
- 02:15 And you could change these to anything you want, if you didn't want them all to be
- 02:19 thick, you could change, you can make a different variable with a thin one or
- 02:23 whatever or a different color one.
- 02:24 So okay, after we've done all of that, put a comma on the last one or not,
- 02:28 it doesn't matter.
- 02:30 Let's go ahead and save this, and let's run this.
- 02:34 So python format.py, then let's open up our pizza.xlsx file and we can see,
- 02:41 Pineapple now has a dark thick sort of thing around it, and very cool.
- 02:47 So that's one way to do this.
- 02:50 Imagine if we wanted to put a dark bar just underneath these two headers, right?
- 02:54 That would be super easy to do.
- 02:55 This is A1 and B1, so let's head back over to our code real quick.
- 02:59 And let's just define A1 equals and this is going to be ws, then in here it's A1.
- 03:07 Let's create another variable called B1 and
- 03:12 set that equal to ws and then B1, and I here,
- 03:17 we could just say, A1.border, =Border and
- 03:22 maybe we just want the bottom to equal our bd.
- 03:28 And I'm going to copy this and just do the same thing, but change this to B1.
- 03:33 So, okay that looks good.
- 03:35 If we save this and run this guy again, and then open up our pizza3 file again,
- 03:41 we can see up here, we've got this nice thick bar going across here
- 03:46 to sort of separate the headers from the rest of the data.
- 03:50 So anything you want you can do, now these are thick borders,
- 03:53 there are many different things you could choose from.
- 03:56 So let's go to style right here, and like I said, let's just type in some gibberish,
- 04:00 and if we run this, then we can see we get an error, but
- 04:03 then it tells us all the different options.
- 04:05 So there's hair, dashDotDot, mediumDashDot, medium, thick dashDot,
- 04:11 slantDash, Dot, Double, medium dash dash, and I'm not going to go
- 04:15 through all these but maybe if we can look at double, sort of a common one.
- 04:20 So let's come up here, and let's change this to double.
- 04:25 Now if we save this, and run this again and
- 04:28 then open our file, You can see now there's double lines, right?
- 04:34 Up here and around here, so very cool.
- 04:36 And like I mentioned, I'm not going to go through all these, you can pause this
- 04:39 video if you want, and make a list of all of these, and check them on your own.
- 04:43 But that's how like we've known in the past, an easy way to get all the options
- 04:47 for you to just to make a mistake and get an error message and see them.
- 04:51 So that's all for this video, and that's all for this section.
- 04:53 In the next section, we're going to move into creating graphs and charts, and
- 04:57 that'll be coming up in the next video.
Lesson notes are only available for subscribers.