HOW TO
It is sometimes necessary to take an inventory of what is in ArcGIS Pro projects just to obtain some simple information, such as the names of the maps and layers in a particular project. This article describes some ways to do this, including a simple script that:
Also included is a slightly more complex script tool that creates a file that lists the names of all the maps and layers in the project.
See the attached file for a sample toolbox.
import arcpy
arcpy.env.workspace = r"<YourOutputDirectory>"
# Create an aprx object
aprx = arcpy.mp.ArcGISProject("CURRENT")
# List maps in the project and select the first one
m = aprx.listMaps()[0]
# List the layers in the map and then store in a variable
lyr = m.listLayers()
# Open a new file and set it to write mode
f = open(r"<YourFileName>.txt", "w")
# Write the name of the map on the first line
f.write("Layers contained in Map: " + m.name + "\n")
f.write("--------------------------------------------------" + "\n")
# Create a for loop to get all of the layer names and then write to the file
for x in lyr:
f.write(x.name + "\n")
# Close out the file
f.close()
# Import modules
import arcpy
# Create user defined variables for script tool
workspace = arcpy.GetParameterAsText(0)
txtFileName = arcpy.GetParameterAsText(1)
# Open a new file and set it to write mode and append a .txt to the input so that a txt file is created
f = open(txtFileName + ".txt", "w")
#Set workspace for output text file
arcpy.env.workspace = workspace
# Create an aprx object
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Write the path of the APRX file to give more context to the output file
f.write("List of Maps and Layers for project located at: " + aprx.filePath + "\n\n")
# Loop through all maps and layers in the project and write to the text file
for m in aprx.listMaps():
f.write("Layers contained in map: " + m.name +"\n")
f.write("--------------------------------------------" + "\n")
for lyr in m.listLayers():
f.write(lyr.name + "\n")
f.write("--------------------------------------------" + "\n\n")
# Close out the file
f.close()
Article ID: 000041430
Get help from ArcGIS experts
Start chatting now