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!
Solved! Go to Solution.
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)
sorry I didn't make the pictures readily visible, here are the ones I mentioned above:
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)
Ha, you beat me by 7 seconds.
Jeff
Ohh thank you so much sir! I just saw your post 😊
Have a lovely week:)
I'm still getting an error saying 'MappingLegendItemObject' object has no attribute 'labelSymbol'.
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
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)