Openpyxl - Adding Borders to Cells in Excel Workbooks with Python | Data Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video demonstrates how to apply borders to cells in an Excel workbook I'll be working off of this empty workbook applying borders to the C4 and C6 cells here I am bringing in load workbook from the Open PI Excel Library I have the Excel workbook loaded into memory as WB with the sheet assigned to variable WS I will be saving my work here with the save function in order to create borders for a cell I need to bring in the border and side objects from the library they are located at the Open PI excel.styles dot borders module each cell has an attribute called border that is a border object this border object accepts side objects as arguments first let's start by accessing the Border attribute of a cell I will do this on cell A1 so that we can compare one cell that I will be working on versus an untouched cell the first thing to notice about this output is that this is a border object this means that I can sign another border object to this attribute and explains why I imported border from the library you can see as well as parameters of the Border object the orientation of the border of the cell are side objects you can see right here with the left right top and Bot interestingly you can also create a diagonal cell border for your cell but for this video I'll be focusing on Left Right top and bottom changing the cell border is done by simply assigning that border attribute a new border object the first argument we're going to be looking at to pass the Border object is the orientation of the border which can be left right top or bottom so let's start with top now as we saw earlier each orientation is a side object so we assign an orientation a new side object with the parameters that we want displayed the specific parameter that we are concerned with is the style argument that we're going to be passing the side object this is going to tell Excel how we want the actual border to be styled let's start with a simple thick border and apply it to the top side of the cell with this applied let's save the workbook and here you can see our first border top portion of C4 has been applied as a thick border thick is not the only style available the documentation lists a wide array of choices that I have added down here as you can see some examples are dash dot dotted lines and slant dash dot lines you can incorporate a mixture of any of these together in a single cell so let's add a dotted line to the right border of C4 all I need to do here is pass right as a border argument Also let's add some color this is done by calling the color argument for the side object down here I have the hex code for Red so let's take this bright red and add it to our right hand side dotted object I simply add color as the Argan let's go ahead save the document and reopen it to see our work here we can see both of our borders have been nicely applied to the C4 object we have our thick border on the top and our dotted red border on the right hand side once you've determined how you want to stylize your border you can apply the styles to all sides all at once so let's change C6 to have a complete red border with the thin Style all I have to do here is copy these two down and make sure all sides are assigned top bottom left right with those changes applied let's go ahead save it and reopen the document and there we see our nicely bordered C6 cell object it has that thin red border along all sides let's take a last look at the actual border attribute of the cell and compare it to this A1 border that we looked at at the start looking at this border object you can see some values are different from the A1 object in A1 you can see that there is a none for style and color for all of the parameters for each of these objects compare that to our C6 border which now has the style of thin and the red hex values for our RGB value for all of the side objects so in conclusion it is quite simple all you have to do is import the border and side objects from the library identify the cell that you want to border on and then assign that border attribute of the cell the Border object which contains side objects with the values for The Styling and the color of your choice thank you for watching I hope you have found this video useful if you enjoyed it and found it helpful in any way please do hit that subscribe button and leave a comment below letting me know what other features you might be interested to review in future videos in the meantime please stay tuned for more videos on features of Open PI Excel
Info
Channel: Data Automation
Views: 1,589
Rating: undefined out of 5
Keywords: Descript
Id: thZWucnV-Bg
Channel Id: undefined
Length: 6min 12sec (372 seconds)
Published: Fri Mar 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.