How to Remove Layouts (PAGX. file) in ArcGIS Pro Projects Using Arcpy?

492
4
05-10-2021 06:24 PM
Labels (1)
MingCao
New Contributor II

 

Hello,

 

I have created 100 layouts that I need to refine their legend one by one using python CIM access. I'm looping through them and my code looks like below:

import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT")

# I stored the 100 features (the "featureclasses") I want to loop through in a gdb called "spp_gdb"
# and I already exported them into layouts using their feature class names respectively (they are in the form of "D:/Pro_Projects/layouts/Layout_"+ fc + ".pagx")

arcpy.env.workspace = r"D:/Pro_Projects/Project_may10th2021/spp_gdb.gdb"
featureclasses = arcpy.ListFeatureClasses()

# create a for loop
for fc in featureclasses:
    aprx.importDocument(r"D:/Pro_Projects/layouts/Layout_"+ fc + ".pagx")
    
    # get the newly imported layout
    x = aprx.listLayouts()[-1]
    # and check its name
    print(x.name)
    
    x_cim = x.getDefinition('V2')
    # here, I did something with python CIM and then I saved my edits
    x.setDefinition(x_cim)
    
    # map layout export
    x.exportToPNG(r"D:/Pro_Projects/Map_"+ fc + ".png", resolution = 300)
    
print("loop finished")

 

However, there seems to be a layout jam in the end since I did not delete each layout I added to the current project every time. The print (x.name) that I used to check the added layout name started to return the same value when it reached layout9. (does it mean 9 layouts maximum in a single Pro project?)

 

This problematic access of layout names affected this automated refinement and I was getting identical map exports in my output folder. I wonder if there is a way to delete added layouts using arcpy scripts in Pro to keep my intended loop functioning?

 

Regards,

Ming

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

if "x" is the layout and you want to delete it did you try

del x

in your script?


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

I just tried it, but it has no effect on stopping the layout name to go up, once I got "layout9" it starts to make replicates for me just like before. it looks like the previous layouts get stored inside the current project..

0 Kudos
TimothyBarnes
Occasional Contributor

Could you do this task another way? 

If the end result needed is just a set of pngs, why not use data driven pages based off the features in your gdb?

 

0 Kudos
MingCao
New Contributor II

Thank you for your reply! But the png set I hope to create is not of different map extents from 1 single layout, rather it's 1 basemap overlaid by one specific species layer each time, I don't think data driven pages can achieve this purpose.

0 Kudos