I'm trying to change the font size for 'xxxMgmt Bnd' layer legend to match the others with ArcPy. I've already tried modifying the patchHeight and patchWidth, but all that does is change the size of the graphic element, not the font size. I can't believe there's not a font property for legend item.
Modifying the 'fittingStrategy' of the Legend Element doesn't work either. It changes every legend item's graphics, not the font size.
Not sure you can.
Actually, this does resize the Legend and the legend items text do appear to be larger. but the font size doesn't change in the format legend appearance. Not sure if this will help.
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/legendelement-class.htm
import arcpy
# Open the current project and layout
aprx = arcpy.mp.ArcGISProject("CURRENT")
layout = aprx.listLayouts("Layout1")[0]
# Specify the name of the legend you want to target
legend_name = "Legend" # Replace with the actual name of your legend
# Find the legend element by name
legend = None
for item in layout.listElements("LEGEND_ELEMENT"):
if item.name == legend_name:
legend = item
break
# Check if the legend was found and modify it
if legend:
# Set the fitting strategy for the legend
legend.fittingStrategy = "AdjustColumnsAndFont" # Or any fitting strategy you need
# Print current element dimensions (height and width)
print(f"Current dimensions of legend: Height = {legend.elementHeight}, Width = {legend.elementWidth}")
# Adjust the element's size
legend.elementHeight = 20 # Set the desired height
legend.elementWidth = 40 # Set the desired width
# Print new dimensions to confirm
print(f"New dimensions of legend: Height = {legend.elementHeight}, Width = {legend.elementWidth}")
else:
print(f"Legend named '{legend_name}' not found.")
Looks like a decent chunk of legend properties are exposed through CIM access. Here's a quick example for setting the legend's title to 22pts:
prj = arcpy.mp.ArcGISProject("CURRENT")
layout = prj.listLayouts()[0]
legend = layout.listElements('LEGEND_ELEMENT')[0]
cim = legend.getDefinition('V3')
cim.titleSymbol.symbol.height = 22
legend.setDefinition(cim)
No gaurentee you can adjust every single component of the legend but it can't hurt to look.