ArcGISProject saveACopy()

4336
3
Jump to solution
11-27-2019 01:36 PM
JaredPilbeam2
MVP Regular Contributor

At the end of this script that imports MXDs into an ArcGIS Pro Project I'm using the saveACopy function.

'''Import folder of MXD(s) into an ArcGIS Pro Project and save the project in a folder.
'''

import arcpy, os

#List full path location of MXD(s) in workspaceMXD  
workspaceMXD = r"C:\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder"
arcpy.env.workspace = workspaceMXD
mxdlist = arcpy.ListFiles("*.mxd")

#aprx project that MXD(s) are being imported into
aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")

for mxd in mxdlist:
    arcpy.env.overwriteOutput = True
    fullpath = os.path.join(workspaceMXD, mxd)
    print("Importing: {}".format(fullpath))
    aprx.importDocument(fullpath) #import the MXD(s) into Pro

    #the count of the maps
    print("Currently {0} maps in Project file.".format(len(aprx.listMaps())))

    #name the maps
    aprx.listMaps()[-1].name = mxd.split("\\")[-1][:-4]

    #save a copy of the project in the workspace folder
    aprx.saveACopy(fullpath)
print("----done----") ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Output

Importing: C:\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder\Union_Elem_81_B.mxd
Currently 2 maps in Project file.
Importing: C:\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder\ValleyView_Unit_365U_B.mxd
Currently 4 maps in Project file.
Importing: C:\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder\WillCounty_Elem_92_B.mxd
Currently 6 maps in Project file.
Importing: C:\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder\Wilmington_Unit_209U_B.mxd
Currently 8 maps in Project file.
----done----

It saves an APRX file from the workspace folder for each imported MXD. But, a copy of the map is imported in each consecutive project. So, as seen here and in the Test Folder, by the fourth saved APRX file maps for the other three projects are also there. 

That's why the file size of the APRXs increases by 400+ KB as the script loops through the folder.

I've also tried taking aprx.saveACopy(fullpath) out of the for loop. But, that only saves one APRX file with all maps in that one project.

How do I loop through the Test Folder and save a copy of the APRX file with it's corresponding map only?

As discussed at the bottom of my other post, ArcGISProject saveACopy seems only to allow full paths as a parameter.

importDocument() OSError 

0 Kudos
1 Solution

Accepted Solutions
LukeWebb
Occasional Contributor III

Its because you have it setup like this: 

#aprx project that MXD(s) are being imported into
aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")

for mxd in mxdlist:
    add to my aprx file

When really, you need it setup like this:


for mxd in mxdlist:
     #Reload the empty "Template project"
     aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")
     
     #import the mxd

     #Save as a copy, but dont overwrite the blank template!

     # (close the template project, so it can be reopened fresh and clean for the next mxd)     
     del aprx 
      

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

I dont know how your maps are getting concatenated, but try explicitly deleting the list by making 'x' the map list, to see if that works.

x = aprx.listMaps()
print("Currently {} maps in Project file.".format(len(x))
#name the maps
...

# line 27.5
del x
0 Kudos
LukeWebb
Occasional Contributor III

Its because you have it setup like this: 

#aprx project that MXD(s) are being imported into
aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")

for mxd in mxdlist:
    add to my aprx file

When really, you need it setup like this:


for mxd in mxdlist:
     #Reload the empty "Template project"
     aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")
     
     #import the mxd

     #Save as a copy, but dont overwrite the blank template!

     # (close the template project, so it can be reopened fresh and clean for the next mxd)     
     del aprx 
      

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Thanks all,

I cleaned it up thanks to your observations and it works great.

'''Import folder of MXD(s) into an ArcGIS Pro Project and save the project in a folder.
'''

import arcpy, os

#List full path location of MXD(s) in workspaceMXD  
workspaceMXD = r"C:\gisfile\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder"
arcpy.env.workspace = workspaceMXD
mxdlist = arcpy.ListFiles("*.mxd")

for mxd in mxdlist:
    #aprx project that MXD(s) are being imported into
    aprx = arcpy.mp.ArcGISProject(r"C:\gisfile\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX.aprx")
   
    fullpath = os.path.join(workspaceMXD, mxd)
    print("Importing: {}".format(fullpath))
    
    #import the MXD(s) into Pro
    aprx.importDocument(fullpath) 
    
    #the count of the maps
    m = aprx.listMaps()
    print("Currently {0} maps in Project file.".format(len(m)))

    #name the maps
    aprx.listMaps()[-1].name = mxd.split("\\")[-1][:-4]

    #save a copy of the project in the workspace folder
    aprx.saveACopy(fullpath)
print("----done----")
del aprx
0 Kudos