Add Support for Guides in arcpy.mp

496
2
03-15-2022 04:27 PM
Status: Already Offered
Labels (1)
BrettFrahm
Occasional Contributor II

I was recently creating and standardizing settings for layouts in bulk and one thing I was hoping to do was set specific guides for them. If this was possible with python I could have set the vertical and horizontal guides to a offset of 0.25 inches for 50 maps with one click.

Tags (3)
2 Comments
JeffBarrette

Hi Brett,

This is a great example of using Python CIM Access: https://pro.arcgis.com/en/pro-app/2.8/arcpy/mapping/python-cim-access.htm

 

p = arcpy.mp.ArcGISProject("CURRENT")

for lyt in p.listLayouts():

lyt_cim = lyt.getDefinition('V2')

newGuides = []

#Bottom horizontal guide
botHorz = arcpy.cim.CreateCIMObjectFromClassName('CIMGuide', 'V2')
botHorz.position = 0.25
botHorz.orientation = "Horizontal"
newGuides.append(botHorz)

#Top horizontal guide
topHorz = arcpy.cim.CreateCIMObjectFromClassName('CIMGuide', 'V2')
topHorz.position = lyt.pageHeight - 0.25
topHorz.orientation = "Horizontal"
newGuides.append(topHorz)

#Left vertical guide
leftVert = arcpy.cim.CreateCIMObjectFromClassName('CIMGuide', 'V2')
leftVert.position = 0.25
leftVert.orientation = "Vertical"
newGuides.append(leftVert)

#Right vertical guide
rightVert = arcpy.cim.CreateCIMObjectFromClassName('CIMGuide', 'V2')
rightVert.position = lyt.pageWidth - 0.25
rightVert.orientation = "Vertical"
newGuides.append(rightVert)

#Add guides and make sure they are turned on
lyt_cim.page.guides = newGuides
lyt_cim.page.showGuides = True

#Set back to layer
lyt.setDefinition(lyt_cim)

Jeff - arcpy.mp and Layout teams

KoryKramer
Status changed to: Already Offered

Marking as Already Offered based on the example Jeff provided showing how to achieve this.