How to turn off headings from legend using python?

1326
5
Jump to solution
05-19-2021 12:10 PM
WesleyCosta
New Contributor II

Well, I'm using ArcGIS Pro 2.7 and I want to turn off the 'Headings' from Legend Item. I know how to do it manually, but I need to make a script. Thanks by attention!

0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor

Hello Wesley,

This can be accomplished using Python CIM Access:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

Here is a snippet of code that does what you need.

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
#Get the layout's CIM definition
lyt_cim = lyt.getDefinition('V2')

#Iterate though all layout elements to find the Legend element
for elm in lyt_cim.elements:
  if elm.name == "Legend":
    #Legend item changes
    for itm in elm.items:
      if itm.name == "GreatLakes":
        #Update visibility
          itm.showLayerName = True
          itm.showHeading = False   

#Set the CIM changes back to the layout
lyt.setDefinition(lyt_cim)

 

There are many Python CIM Access samples located here (including legenditem changes):

https://www.arcgis.com/home/item.html?id=8772f61319584882bb697ba003030636

 

Jeff - arcpy.mp and Layout teams

View solution in original post

Tags (1)
5 Replies
DanPatterson
MVP Esteemed Contributor

Did you look at the CIM class?  For example

LegendElement—ArcGIS Pro | Documentation

LegendItem—ArcGIS Pro | Documentation

plus others.  

You didn't indicate what you had looked at.


... sort of retired...
0 Kudos
WesleyCosta
New Contributor II

Sem título.png

I've ever seen the Documentation above.

0 Kudos
DanPatterson
MVP Esteemed Contributor

If it isn't on the CIM stuff, it may not be available yet.


... sort of retired...
JeffBarrette
Esri Regular Contributor

Hello Wesley,

This can be accomplished using Python CIM Access:

https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

Here is a snippet of code that does what you need.

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts()[0]
#Get the layout's CIM definition
lyt_cim = lyt.getDefinition('V2')

#Iterate though all layout elements to find the Legend element
for elm in lyt_cim.elements:
  if elm.name == "Legend":
    #Legend item changes
    for itm in elm.items:
      if itm.name == "GreatLakes":
        #Update visibility
          itm.showLayerName = True
          itm.showHeading = False   

#Set the CIM changes back to the layout
lyt.setDefinition(lyt_cim)

 

There are many Python CIM Access samples located here (including legenditem changes):

https://www.arcgis.com/home/item.html?id=8772f61319584882bb697ba003030636

 

Jeff - arcpy.mp and Layout teams

Tags (1)
WesleyCosta
New Contributor II

Thanks a lot, Jeff!

0 Kudos