Openpyxl - Filling Cells with Color in Excel Workbooks with Python | Data Automation

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video will demonstrate how to color cells using the python Library open Pi Excel here we have the blank document with which I am going to start filling with some colors I start by importing my load workbook function from Open PI Excel I'm going to assign the WB variable my workbook and I'm going to select the worksheet that I want to work with call that ws and I'll be saving my work down here that's actually test not text Dot xlsx since we haven't done anything yet let's just open some more cells down here now there are a few things that I have to do to access the actual filling content of these cells themselves the first I have to do is create a pattern fill object I have to create this pattern fill object from the Open PI Excel dot Styles Library so I'll be bringing that in first from Open PI Excel dots styles import pattern fill this is a function that exists in that style module and it accepts a couple of arguments so the first thing that I want to do is I'm going to create an object I'm going to call it green because one of the cells we will fill with green I'm going to create a pattern oops fill object and it accepts a number of arguments here the first being the fill type that I want to provide for our case we're going to look at filling it completely so we're going to do a solid fill and then it also accepts a color the way that it does this is it identifies two different color types A start color and an N color since we're only going to be filling with the one color all you have to provide as an argument is a single color I pulled up this hex code website and I've selected let's go ahead and select this green as a green color one three three seven hundred provide that as the start color and let's find a nice blue as well let's go ahead and select this blue so we now have two different pattern fill objects one one of blue one of green coloring the actual cells themselves is fairly straightforward all you have to do is access the cell that you want to affect for our case let's go with C4 and D5 cells in Excel have a DOT fill attribute I will provide that attribute right here for the worksheet and all both of these are going to equal are the color that we want to assign so C4 we will provide the green pattern fill object D5 is going to be the blue pattern fill object I'll run the sky so far that looks successful let's save the document and see what happens here you can see the filled cells with the colors that we expected green at C4 and blue at D5 so it is quite simple all you have to do is pull in the pattern fill function that accepts a number of arguments one of them being the fill type that you want to provide the second being the color that you're going to fill the cell with you then assign the dot fill attribute of the cells you're interested in the color that you want to assign them thank you for watching I hope this was helpful and informative please stay tuned for some more videos that are coming up on open Pixel
Info
Channel: Data Automation
Views: 2,040
Rating: undefined out of 5
Keywords: Descript
Id: 0AlQtxFqv54
Channel Id: undefined
Length: 3min 51sec (231 seconds)
Published: Wed Mar 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.