MM84
MM84

Reputation: 1

Replaceformat with xlwings

does xlwings support replace_format?

I have a large array 200 rows and 250 columns and if I try to color each cell that has a value of “car” to red, it will take so much time to loop through each cell to color only the ones with value of a car.

So I was thinking of replace_format and use fill to change the color.

Does xlwings support replace_format? If not what’s a good way to do it?

Upvotes: 0

Views: 87

Answers (1)

moken
moken

Reputation: 6620

The detail on what you have tried is very vague and would likely get your Post closed for same reason.
I don't know what you have attempted to achieve your desired result and do not know what "if try to color each cell with a value of “input” to red" means.

However to answer your question "does xlwings support replace_format?", yes it does.

For Example
To find all cells that are GREEN and replace with RED the following code can be used;
In this example the code searches the range 'A1:IP200' and replaces fill in any cells where the fill color is GREEN.
You can also specify the used range instead;
ws.used_range.api.Replace(...
In the code we are only specifying the fill colour for search and replace formatting however other formatting could also be applied to FindFormat and ReplaceFormat

import xlwings as xw
from xlwings.constants import LookAt, SearchOrder
from xlwings.utils import rgb_to_int

filename = 'foo.xlsx'
sheet = 'Sheet1'

with xw.App(visible=True) as xl:
    wb = xl.books.open(filename)
    ws = wb.sheets[sheet]

    ### Set the find format; Colour = GREEN (0, 255, 0)
    ws.api.Application.FindFormat.Interior.Color = rgb_to_int((0, 255, 0))
    ### Set the replace format; Colour = RED (255, 0, 0)
    ws.api.Application.ReplaceFormat.Interior.Color = rgb_to_int((255, 0, 0))

    ### Run the Replace. 
    ws.range('A1:IP200').api.Replace(
                              What="",
                              Replacement="",
                              LookAt=LookAt.xlPart,
                              SearchOrder=SearchOrder.xlByRows,
                              MatchCase=False,
                              SearchFormat=True,
                              ReplaceFormat=True,
                              )

    wb.save(filename)

Update
If you want to set the fill colour to RED for cells that have the value 'car' then use the code below. This example will retain the value (.i.e. 'car') in the cell;
Ensure to set the case sensitivity if needed


import xlwings as xw
from xlwings.constants import LookAt, SearchOrder
from xlwings.utils import rgb_to_int

filename = 'foo.xlsx'
sheet = 'Sheet1'

with xw.App(visible=True) as xl:
    wb = xl.books.open(filename)
    ws = wb.sheets[sheet]

    ### Set the replace format; Colour = RED (255, 0, 0)
    ws.api.Application.ReplaceFormat.Interior.Color = rgb_to_int((255, 0, 0))

    ### Run the Replace. 
    ws.range('A1:IP200').api.Replace(
                              What="car",
                              Replacement="",
                              LookAt=LookAt.xlPart,
                              SearchOrder=SearchOrder.xlByRows,
                              MatchCase=False,
                              SearchFormat=False,
                              ReplaceFormat=True,
                              )

    wb.save(filename)

Upvotes: 1

Related Questions