Arcpy Mapping

471
2
12-28-2022 06:26 PM
ChrisJRoss13
New Contributor III

Hi;

I am trying to create a new Pro Project from a previous template.

I have a couple questions:

  1. When I update the default toolbox, it gets added to the project as default but the old toolbox is still there referenced. Is there any way to remove it in Python without deleting it?
  2. Similar to above, when I updated the Home Folder, it gets added but the old folder is still there. Is there any way to remove it in Python without deleting it?
  3. My main concern, when I update the default database, it does not show initially and is not added automatically to the list of databases. Is there anyway to add it via Python and remove the old one, without deleting it? When I add the new default database connection manually it does show it is the default gdb.

I have included a couple screenshots of the Pro Project Catalog Pane after the script is completed and then once I manually add the new gdb connection.

I have also pasted my code below. I removed file paths as these are not pertinent for the issue.

Any help would be appreciated!

import arcpy
import os
import shutil

# Main Downloads
downloads_fol = r'C:\Users\rossch\Downloads'

# ArcGIS Prp template to copy
pro_template = r'FULL PATH TO PRO APRX TEMPLATE'

# Specify the ArcGIS Pro reference
pro_temp_aprx = arcpy.mp.ArcGISProject(pro_template)

# Ask the user for the Pro project name; set the new project path and save a copy
pro_name = input('Enter the name of the ArcGIS Pro folder and project to create: ')

# Specify the folder name and if it doesn't exist, create it
pro_prj_fol = os.path.join(downloads_fol, f'{pro_name}')
if not os.path.exists(pro_prj_fol):
os.mkdir(pro_prj_fol)

# Specify the new project name
new_pro_prj = os.path.join(pro_prj_fol, f'{pro_name}.aprx')

# Save a copy of the template ArcGIS Pro project
pro_temp_aprx.saveACopy(new_pro_prj)

# Clean up the original template ArcGIS Pro project
del pro_temp_aprx

# Set the new copied project as the ArcGIS Pro reference
main_aprx = arcpy.mp.ArcGISProject(new_pro_prj)

# Set the home folder
main_aprx.homeFolder = pro_prj_fol

# Create a new file geodatabase and make that the default gdb for the project
prj_gdb = os.path.join(pro_prj_fol, f'{pro_name}.gdb')
arcpy.CreateFileGDB_management(pro_prj_fol, f'{pro_name}.gdb')
main_aprx.defaultGeodatabase = prj_gdb

# Copy the template toolbox and make the default for the project
orig_tbx = r'FULL PATH TO TOOLBOX'
new_pro_tbx = os.path.join(pro_prj_fol, f'{pro_name}.atbx')
shutil.copy(orig_tbx, new_pro_tbx)
main_aprx.defaultToolbox = new_pro_tbx

# Save the project
main_aprx.save()

# Start the copied ArcGIS Pro project
os.startfile(new_pro_prj)
0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

for the toolbox RemoveToolbox—ArcGIS Pro | Documentation

as for projects in general, you can create a template that creates new folders, gdb's and a toolbox or uses ones you specify (project backstage, options, General, Create Projects).

Would changing any of those when creating the project template help?


... sort of retired...
0 Kudos
ChrisJRoss13
New Contributor III

Appreciate the reply @DanPatterson.

Unfortunately, the Remove Toolbox geoprocessing tool only removes the Toolbox from the geoprocessing session and not from the ArcGIS Pro Project.

I am aware that you can set a pre-defined gdb, toolbox, etc. when Projects are created but then the copied Pro Project will just reference those and I am in the same boat.

0 Kudos