Format Legend with Arcpy

459
4
04-27-2021 12:46 PM
Labels (1)
anamanvil
New Contributor II

Hi All, 

I am trying to set a specific format from the legend. Ideally, I would like to assign a Font Size and Font Colour for both Group Layer Name and the Layer Label within it. 

Unfortunately, even though I set the below parameters with my preference in the Desktop, as soon as I add/refresh the layer within this legend, the Font Colour and Size is again as default being black.

Could you please let me know how to fix this or if there is a way to amend this dynamically using arcpy?

 

UDPATE: I've just found that I need to set this as Default within the legend item by right clicking over it. I've not find a way to do this with arcpy; so please feel free to share it here even though I found a workaround :)

 

anamanvil_0-1619552620403.png

 

4 Replies
DanPatterson
MVP Esteemed Contributor

Your code is missing


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

Just updated the post. Unfortunately I have not been able to find out how to access this through arcpy, so no code is shared. I've just tried it manually.

0 Kudos
JeffBarrette
Esri Regular Contributor

Anamanvil,

I can provide a sample Python script using Python CIM access but 1) I really want to know more about the UI and why it is automatically readjusting.  What is your fitting strategy?  If you have auto adjust font size, I would expect font size to ignore your settings because the legend was set to auto adjust.  2) Python CIM access is slightly more advanced.  Are you familiar with arcpy.mp?  Again, I'm a happy to send a snippet but I want to make sure there is not a bug in the application.

Jeff - arcpy.mp and Layout teams

 

0 Kudos
JeffBarrette
Esri Regular Contributor

Here is a snippet that changes the group layer name size and color. For more information on Python CIM access:  https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/python-cim-access.htm

 

lyt.setDefinition(lyt_cim)
arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts('Layout_MS')[0]
lyt_cim = lyt.getDefinition('V2')
for e in lyt_cim.elements:
  if e.name == 'Legend':
    for itm in e.items:
      if itm.name == 'GreatLakes':
          itm.groupLayerNameSymbol.symbol.height = 24  #Change font size
          itm.groupLayerNameSymbol.symbol.symbol.symbolLayers[0].color.values = [255,255,20, 100] #change RGB color
lyt.setDefinition(lyt_cim)

 Jeff - arcpy.mp and Layout teams