Select to view content in your preferred language

Converting VBScript Label Expression to Python

397
6
Jump to solution
11-05-2025 05:40 AM
sawillis96
Emerging Contributor

Hello All,

I am having trouble trying to convert VBScript label expressions to Python. I've tried running them through AI to help convert, but no luck. Is there a way within the ArcGIS Pro software to complete this?

 

Thanks!

1 Solution

Accepted Solutions
TonyAlmeida
MVP Regular Contributor

Untested but try something like this, 

python phrase,

def LabelBlockLineFormatter(Descripter, Value):
    if Descripter == "Above Criteria":
        return f"<CLR red='255'>{Value}</CLR>"
    elif Value == 0:
        return "ND"
    elif Value is None or Value == "<Null>":
        return "NS"
    else:
        return str(Value)

def FindLabel(F1, Total_VOC_Jun_2017, Total_VOC_Notes_Jun_2017, 
              Total_VOC_Oct_2017, Total_VOC_Notes_Oct_2017, 
              Total_VOCs_2019_08, Total_VOCs_Notes_2019_08, 
              Total_VOC_April_2022, Total_VOC_Notes_April_2022):

    label = f"<BOL><UND>{F1}</UND></BOL>"
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_Jun_2017, Total_VOC_Jun_2017)
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_Oct_2017, Total_VOC_Oct_2017)
    label += "\n" + LabelBlockLineFormatter(Total_VOCs_Notes_2019_08, Total_VOCs_2019_08)
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_April_2022, Total_VOC_April_2022)

    return label

 

expression line

FindLabel(!F1!, !Total_VOC_Jun_2017!, !Total_VOC_Notes_Jun_2017!, 
          !Total_VOC_Oct_2017!, !Total_VOC_Notes_Oct_2017!, 
          !Total_VOCs_2019_08!, !Total_VOCs_Notes_2019_08!, 
          !Total_VOC_April_2022!, !Total_VOC_Notes_April_2022!)

 

View solution in original post

0 Kudos
6 Replies
TonyAlmeida
MVP Regular Contributor

It would help if you posted what you are trying to convert.

sawillis96
Emerging Contributor

I was just curious if there was a method within ArcGIS Pro to automatically convert it. But here is the VBS code I'm working with.

 

Function FindLabel ( [F1], [Total_VOC_Jun_2017], [Total_VOC_Notes_Jun_2017], [Total_VOC_Oct_2017], [Total_VOC_Notes_Oct_2017], [Total_VOCs_2019_08], [Total_VOCs_Notes_2019_08], [Total_VOC_April_2022], [Total_VOC_Notes_April_2022])
FindLabel="<BOL><UND>" & [F1] & "</UND></BOL>"
FindLabel = FindLabel & vbNewLine & LabelBlockLineFormatter([Total_VOC_Notes_Jun_2017], [Total_VOC_Jun_2017])
FindLabel = FindLabel & vbNewLine & LabelBlockLineFormatter([Total_VOC_Notes_Oct_2017], [Total_VOC_Oct_2017])
FindLabel = FindLabel & vbNewLine & LabelBlockLineFormatter([Total_VOCs_Notes_2019_08], [Total_VOCs_2019_08])
FindLabel = FindLabel & vbNewLine & LabelBlockLineFormatter([Total_VOC_Notes_April_2022], [Total_VOC_April_2022])
End Function

Function LabelBlockLineFormatter([Descripter], [Value])
if([Descripter] = "Above Criteria") then
LabelBlockLineFormatter = "<CLR red = '255'>" & [Value] & "</CLR>"
elseif ([Value] = 0) then
LabelBlockLineFormatter = "ND"
elseif isnull([Value]) then
LabelBlockLineFormatter = "NS"
else
LabelBlockLineFormatter = [Value]
end if
End Function

0 Kudos
TonyAlmeida
MVP Regular Contributor

Untested but try something like this, 

python phrase,

def LabelBlockLineFormatter(Descripter, Value):
    if Descripter == "Above Criteria":
        return f"<CLR red='255'>{Value}</CLR>"
    elif Value == 0:
        return "ND"
    elif Value is None or Value == "<Null>":
        return "NS"
    else:
        return str(Value)

def FindLabel(F1, Total_VOC_Jun_2017, Total_VOC_Notes_Jun_2017, 
              Total_VOC_Oct_2017, Total_VOC_Notes_Oct_2017, 
              Total_VOCs_2019_08, Total_VOCs_Notes_2019_08, 
              Total_VOC_April_2022, Total_VOC_Notes_April_2022):

    label = f"<BOL><UND>{F1}</UND></BOL>"
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_Jun_2017, Total_VOC_Jun_2017)
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_Oct_2017, Total_VOC_Oct_2017)
    label += "\n" + LabelBlockLineFormatter(Total_VOCs_Notes_2019_08, Total_VOCs_2019_08)
    label += "\n" + LabelBlockLineFormatter(Total_VOC_Notes_April_2022, Total_VOC_April_2022)

    return label

 

expression line

FindLabel(!F1!, !Total_VOC_Jun_2017!, !Total_VOC_Notes_Jun_2017!, 
          !Total_VOC_Oct_2017!, !Total_VOC_Notes_Oct_2017!, 
          !Total_VOCs_2019_08!, !Total_VOCs_Notes_2019_08!, 
          !Total_VOC_April_2022!, !Total_VOC_Notes_April_2022!)

 

0 Kudos
sawillis96
Emerging Contributor

Had to do some minor tweaks but this worked! Thank you so much! 

0 Kudos
TonyAlmeida
MVP Regular Contributor

Right on! Happy to help!

0 Kudos
HaydenWelch
MVP Regular Contributor

Best way is to just manually port them over, but we can't help without the original script as Tony said

0 Kudos