HarryKane
HarryKane

Reputation: 168

FormatConditions.Add(Type:=xlTextString, TextOperator:=xlContains, String:="myText")

I have a VBA code, which I want to convert into my python script.

Sub formatStatus()

Dim ws As Worksheet
Dim fc_blocked As FormatCondition

Set ws = ThisWorkbook.ActiveSheet()

ws.Columns(1).FormatConditions.Delete

Set fc_blocked = ws.Columns(1).FormatConditions.Add(Type:=xlTextString, TextOperator:=xlContains, String:="blocked")
fc_blocked.Font.Color = RGB(100, 48, 160)
fc_blocked.Interior.Color = RGB(180, 120, 250)

End Sub

My python code looks like this:

import win32com.client as win32

fcBlocked = ws.Columns(iCol).FormatConditions.Add(Type = win32.constants.xlTextString, TextOperator=win32.constants.xlContains, String='blocked')
fcBlocked.Font.Color = int('%02x%02x%02x' % (100, 48, 160)[::-1],16)
fcBlocked.Interior.Color = int('%02x%02x%02x' % (180, 120, 250)[::-1],16)

But I get an exception.

I successfully tried

fcBlocked = ws.Columns(iCol).FormatConditions.Add(Type = win32.constants.xlCellValue, TextOperator=win32.constants.xlEqual Formula1='="blocked"')
fcBlocked.Font.Color = int('%02x%02x%02x' % (100, 48, 160)[::-1],16)
fcBlocked.Interior.Color = int('%02x%02x%02x' % (180, 120, 250)[::-1],16)

but this checks if the text is equal, and not if the string contains 'blocked'.

Can someone give me an advice?

Thanks in advance and best regards!

Upvotes: 2

Views: 3592

Answers (1)

Pavel K
Pavel K

Reputation: 51

Just spent an hour figuring out the answer, almost surrendered but finally found it:

xlFC = xlApp.Columns("G:G").FormatConditions.Add(xlTextString, None, None, None, u'осталось -', xlContains, None, None)

Upvotes: 5

Related Questions