Color specific cell in an xlsx with python

0 votes

 I want to color cell in an xlsx doc in terms of value

Here's an example

1

I want to color all the "Alexandre"

For that, I created this:

path_to_excel="doc.xlsx"
exemple=openpyxl.load_workbook(path_to_excel)


ws = exemple['Sheet1'] #Name of the working sheet

from openpyxl.styles import PatternFill

#on définie la couleur de coloriage en hexa
Ok_couleur = PatternFill(patternType='solid', 
                            fgColor='94e0de')

for column in ws['B']:
        if column.value == "Alexandre":
            ws['B3'].fill = Ok_couleur

I only coloured one cell, but I want to colour all of them with Alexandre.

I tried to change the B3 with B in this line, but I was unsuccessful.

for column in ws['B']:
        if column.value == "Alexandre":
            ws['B3'].fill = Ok_couleur`
Jan 14, 2023 in Others by Kithuzzz
• 38,010 points
1,543 views

1 answer to this question.

0 votes

Column B's cells must be iterated through in order to determine whether the value is "Alexandre." If it is, you can give that cell the fill colour.

import openpyxl
path_to_excel="output.xlsx"
exemple=openpyxl.load_workbook(path_to_excel)
ws = exemple['Sheet1'] #Name of the working sheet
from openpyxl.styles import PatternFill 

#on définie la couleur de coloriage en hexa
Ok_couleur = PatternFill(patternType='solid', fgColor='94e0de')
for column in ws['B']:
    if column.value == "Alexandre":
        column.fill = Ok_couleur
exemple.save(path_to_excel)

the output before coloring : enter image description here

after coloring enter image description here

answered Jan 14, 2023 by narikkadan
• 63,620 points

Related Questions In Others

0 votes
1 answer

Removing specific rows in an Excel file using Azure Data Factory

Under the 'Source' tab, choose the number ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,620 points
2,070 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24, 2022 in Others by narikkadan
• 63,620 points
902 views
0 votes
1 answer

IF function in combination with an Round function Excel

I'm not sure if it is a ...READ MORE

answered Sep 25, 2022 in Others by narikkadan
• 63,620 points
2,603 views
0 votes
1 answer

Is there any way in python to auto-correct spelling mistake in multiple rows of an excel files of a single column?

Use Spellchecker for doing your stuff: import pandas ...READ MORE

answered Oct 14, 2022 in Others by narikkadan
• 63,620 points
1,617 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,093 views
0 votes
1 answer
0 votes
1 answer

How to stick an embedded document in a specific cell of an excel

Solution Select the documents (you can use the ...READ MORE

answered Oct 18, 2022 in Others by narikkadan
• 63,620 points
448 views
0 votes
1 answer

Make certain characters in an Excel cell subscript Python

With Xlwings, you may accomplish this by ...READ MORE

answered Dec 13, 2022 in Others by narikkadan
• 63,620 points
868 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP