Select to view content in your preferred language

How to Increase the Legend Item Font Size Using Python Access CIM in ArcGIS Pro ?

3283
7
Jump to solution
05-10-2021 11:02 AM
MingCao
Occasional Contributor

 

Hello:)

I'm running a map automation to create over 100 species maps that share a common base map but vary with the species points showed each time. I have successfully created the maps, and exported them as PNG using arcpy, but on each map export the legend font size is really tiny (default font size 10). I want to increase the font for better visualization. 

 

I  found that arcpy does not contain a method to modify the legend item font size, so I used CIM in the hope to access more detailed legend property settings. (the red arrow in picture 1 below is the place I want to modify through CIM).

 

Here's what I tried:

I exported one of the species map, Arthrosporum populorum,  as a PAGX file and I viewed it as a JSON file in a text editor. Picture 2 shows that starting from line 4643 is the place I'm interested in ("Arthrosporum populorum_layer is a point feature layer"). However, I didn't see anything such as a "fontSize" key that allows me to modify. Have I searched for the wrong object place, or are there any other places I should be looking for as well?

 

I really appreciate your help!

 

0 Kudos
1 Solution

Accepted Solutions
MingCao
Occasional Contributor

I just figured out myself, but thank you for checking! 

import arcpy

# access the current map project, and the newly added layout(called x)
aprx = arcpy.mp.ArcGISProject("CURRENT")
x = aprx.listLayouts()[-1]
x.name

# access the CIM of this layout
x_cim = x.getDefinition('V2')

# change the legend font size to 26
for itm in x_cim.elements:
    if itm.name == "Legend":
        for species in itm.items:
            species.labelSymbol.symbol.height = 26

# save the CIM edits 
x.setDefinition(x_cim)

 

View solution in original post

0 Kudos
7 Replies
MingCao
Occasional Contributor

sorry I didn't make the pictures readily visible, here are the ones I mentioned above:

pic 1.jpg

 

pic 2.png

0 Kudos
MingCao
Occasional Contributor

I just figured out myself, but thank you for checking! 

import arcpy

# access the current map project, and the newly added layout(called x)
aprx = arcpy.mp.ArcGISProject("CURRENT")
x = aprx.listLayouts()[-1]
x.name

# access the CIM of this layout
x_cim = x.getDefinition('V2')

# change the legend font size to 26
for itm in x_cim.elements:
    if itm.name == "Legend":
        for species in itm.items:
            species.labelSymbol.symbol.height = 26

# save the CIM edits 
x.setDefinition(x_cim)

 

0 Kudos
JeffBarrette
Esri Regular Contributor

Ha, you beat me by 7 seconds. 

Jeff 

0 Kudos
MingCao
Occasional Contributor

Ohh thank you so much sir! I just saw your post 😊

Have a lovely week:)

0 Kudos
MitchellBlacquiere
New Contributor

I'm still getting an error saying 'MappingLegendItemObject' object has no attribute 'labelSymbol'.

0 Kudos
JeffBarrette
Esri Regular Contributor

Can you provide the snippet of code you are using AND perhaps a screenshot of the legend and legenditem that is not working as expected?  I see above that you noted that you figured it out but I'm not sure if that is current.

Jeff

0 Kudos
JeffBarrette
Esri Regular Contributor

Hello MingCao,

Below is a snippet of code that modifies a layer's legend item label size using Python CIM access.  Note - you may not get the results you expect if your Legend is using a fitting strategy that involves automatically adjusting font size.  Also be careful about setting LayerNames vs Labels font size.  It depends on the properties of the legend which ones are displayed.

Uncomment the last two lines if you want to change the font size.  

p = arcpy.mp.ArcGISProject('current')
lyt = p.listLayouts('Layout_MS')[0]
lyt_cim = lyt.getDefinition('V2')
for elm in lyt_cim.elements:
  if elm.name == "Legend":
    print(elm.fittingStrategy)
    for itm in reversed(elm.items):
      if itm.name == "GreatLakes":
        print(itm.labelSymbol.symbol.height)
        #itm.labelSymbol.symbol.height = 9.99
#lyt.setDefinition(lyt_cim)

 

0 Kudos