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

1575
7
05-10-2021 06:24 PM
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
7 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
GeoNZ
by
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
VíctorAE
New Contributor II

I know it has been a long time since the post was written but, did you solve the problem? I have the same issue with an imported layout in a loop

Thanks

0 Kudos
MingCao
New Contributor II

@VíctorAEhi! 

I didn't solve this problem back then (I'm not sure  if ArcPy has added specific functions for this purpose recently), but I went around the issue by pre-setting my layout (the basemap layers, the north arrow, scale bar etc.) as a template project. This worked for me since I was only dealing with one specific layout, and it's used repeatedly. Good luck!
0 Kudos
VíctorAE
New Contributor II

Hi! My ArcPy does not have implemented that functionality. I tried to use a layout template as you did, but I have a table created from scratch and, I don´t know why, but the reference is lost in the iteration

Thanks a lot!

0 Kudos