How to add colour to Excel cells using Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report To add color to Excel cells we will be using the Openpyxl Python Library. The Openpyxl module allows us to read and modify Excel files using Python. Approach 1: Using the Openpyxl module, even small tasks can be done very efficiently and easily in excel. Input File: Only declare the excel name with .xlsx form if the file exists in the same folder as where the code exists. If the file exists in another folder then the linking has to be done by giving the entire path source. Installing the OpenPyxl module: In order to install the openpyxl module via pip run the below command in the terminal of the user's choice: pip install openpyxl Example: Python3 import openpyxl from openpyxl.styles import PatternFill wb = openpyxl.load_workbook("GFGCoursePrices.xlsx") ws = wb['Sheet1'] To fill the color in the cell you have to declare the pattern type and fgColor. Now create a working sheet variable and initialize it with the current sheet you are working on using the syntax Python3 colors = ['00660066', '00FFFFCC', '00FF0000', '0000FF00', '00660066'] fillers = [] for color in colors: temp = PatternFill(patternType='solid', fgColor=color) fillers.append(temp) Here we need to keep in mind that colors value has to be strictly hex values not like 'Blue' ,'yellow' , 'red' etc. It will not work. Otherwise, we will encounter an error like below: Finally, you have to save changes using the .save() function. To fill each cell, you have to use the .fill() function depicted by the following syntax. Python3 cell_ids = ['B2', 'B3', 'B4', 'B5', 'A2'] for i in range(5): ws[cell_ids[i]].fill = fillers[i] wb.save("GFGCoursePrices.xlsx") Output: Approach 2: We can also take the help of Color method of the openpyxl.styles.colors. Here we can pass integer values (0-63) and while coloring them we need to pass them as the value of a keyworded argument indexed. Python3 import openpyxl from openpyxl.styles import PatternFill from openpyxl.styles.colors import Color wb = openpyxl.load_workbook("geeks.xlsx") ws = wb['Sheet1'] # Color Indexes, ranges from 0-63 colors = [6, 3, 48, 37, 28] fillers = [] for color in colors: temp = PatternFill(patternType='solid', fgColor=Color(indexed=color)) fillers.append(temp) cell_ids = ['B2', 'B3', 'B4', 'B5', 'A2'] for i in range(5): ws[cell_ids[i]].fill = fillers[i] wb.save("geeks.xlsx") Output: Comment More infoAdvertise with us Next Article How to add colour to Excel cells using Python R raj2002 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads How to add colour to text Python? There are multiple ways supported by python in which color can be added to text. This article discusses all with proper examples to help you understand better. Method 1: Using ANSI ESCAPE CODE ANSI escape sequence is a sequence of ASCII characters, the first two of which are the ASCII "Escape" chara 2 min read Convert Excel to PDF Using Python Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry.In this article, we will learn how to convert an 1 min read How to replace a word in excel using Python? Excel is a very useful tool where we can have the data in the format of rows and columns. We can say that before the database comes into existence, excel played an important role in the storage of data. Nowadays using Excel input, many batch processing is getting done. There may be the requirement o 3 min read Add New Sheet to Excel Using Pandas The article aims to implement a function in Python using Pandas to add a new sheet to an existing Excel file, facilitating efficient data organization and management within Excel workbooks. Using openpxl to add a new sheet to Excel Using PandasPandas offer a seamless interface for manipulating Excel 2 min read How to Color Cells with write.xlsx The xlsx package in R provides functions that can be used to read, write and format excel files. In this article, we will look at one such use of the xlsx package to format and basically color the cells of an XLSX file using the XLSX package and R Programming Language. Coloring Cells using XLSX Pack 5 min read Merge and Unmerge Excel Cells using openpyxl in Python In this article, we are going to learn about Python programs to merge and unmerge excel cells using openpyxl.IntroductionOpenpyxl is a Python library commonly used for reading and writing Excel Files. In this article, we will be using this library to merge cells in Excel. Merging is helpful when we 4 min read How to Convert Excel to XML Format in Python? Python proves to be a powerful language when the requirement is to convert a file from one format to the other. It supports tools that can be employed to easily achieve the functionality. In this article, we'll find out how we will convert from an Excel file to Extensible terminology (XML) files wit 3 min read How to add timestamp to excel file in Python In this article, we will discuss how to add a timestamp to an excel file using Python. Modules requireddatetime: This module helps us to work with dates and times in Python.pip install datetimeopenpyxl: It is a Python library used for reading and writing Excel files.pip install openpyxltime: This mo 2 min read Writing to an excel sheet using Python Using xlwt module, one can perform multiple operations on spreadsheet. For example, writing or modifying the data can be done in Python. Also, the user might have to go through various sheets and retrieve data based on some criteria or modify some rows and columns and do a lot of work. Let's see how 2 min read How to Change the Color of a Graph Plot using pygal? Prerequisites: pygal Pygal is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. In this article, we will see how we can change chart color in the Pygal module. While making a chart it is important for us to adjust co 2 min read Like