Five Python Code Snippets for ArcGIS Pro Mapping Tasks

Blog Post created by sboden-esristaff Employee on Feb 1, 2017

A previous post shared five Python code snippets to automate mapping tasks in ArcMap. This post walks you through how to accomplish those same tasks using the ArcGIS Pro Python window.

In ArcGIS Pro, the mapping module is called To try the snippets below for yourself, make a copy of an ArcGIS Pro project file (APRX) and move it somewhere for testing purposes. You don’t have to be an experienced Python scripter—simply enter the code below into the Python window and check out the results. 

You’ll soon see how a few Python tricks can speed up map management.

  • Each snippet below works with ArcGIS Pro 1.x, an existing project file, module, and Python 3.x.

Snippet 1: Check Layer Definition Queries


Suppose you need to check if any of the layers in your ArcGIS Pro project have a definition query applied, and if so, view the definition queries. Opening the Layer Properties dialog box for each layer in each map is a pain.


Solution: Open the Python window instead and run the code below. The name and definition query of all the layers will be “printed” (i.e., displayed in the Python window).

Python code snippet to display layer definition queries

Code Breakdown

  • The first line of code creates a variable called aprx that references the open project using the ArcGISProject function with the CURRENT keyword.
  • The next line of code creates a variable called maps using the listMaps method that returns a Python list of Map objects in the project.
  • A for loop is used to iterate through each map.
  • For each map, the listLayers method is used to create a list of the layers.
  • Another for loop is used to iterate through the list of layers.
  • The Layer class has many properties that can be accessed, including the name and definition query of a layer. Because not all layer types support all properties, the supports method is used to test which properties a layer supports.
  • The last line of code prints (displays) the name of each map, the name of each layer, and each layer's definition query.

Snippet 2: Fix Broken Links

At some point, you have probably moved map data from one workspace to another. Have you ever seen the dreaded red exclamation marks?

ArcGIS Pro Contents pane showing broken layer data source paths

If the path to map layers has changed, with just a few lines of code you can quickly find the old workspace path and replace it with the new workspace path for all layers and tables in your project at once. Run the following lines of code in the Python window (be sure to update the paths to reflect your old and new workspaces).

Python code snippet to repair broken layer data source paths

Code Breakdown

  • The first line of code is the same as the previous example, creating the variable aprx that references the open ArcGIS Pro project.
  • The updateConnectionProperties method is used to replace the current connection information (first parameter) with new connection information (second parameter) for all layers and tables in the project.
  • Learn more: Updating and fixing data sources

Snippet 3: Identify Layers with a Broken Data Source

Speaking of those red exclamation marks, you can also use to print out the layers that have broken links using the code below.

Python code snippet to find layers with broken layer data source paths

Code Breakdown

  • The first line of code creates a variable called aprx that references the open project, just like the examples above.
  • Like the first example, the listMaps and listLayers methods are used to create a list of layers in each map in the project.
  • The for loops are used to iterate through each layer in each of the maps, printing out the name and isBroken layer properties for each. The “\n” adds a line before the map name and creates a separation between the layers for each map.

Snippet 4: Inventory Layer Data Sources

If you need to know the data source for all the layers in your project, but don’t want to open the Layer Properties dialog box for each, simply run the lines of code below in the Python window to print out the names and data sources of the layers.

Python code snippet to display maps layers with their data sources

Code Breakdown

  • This example starts the same way as the previous snippet, creating a variable for the project (aprx), and creating lists of the maps and layers in each of the maps.
  • In this example, you print out the name and dataSource properties for each layer in your map. The supports method is used because not all layers support the dataSource property.

Snippet 5: Combine PDF Documents

Maybe you’re creating a map book or maybe you have a few PDF documents you need to combine (like your expense report and receipts). You can use and the PDFDocument class to manipulate PDF documents. Run the code below to combine two PDF documents.

Python code snippet to append PDF files

Code Breakdown

  • The first line of code creates a variable called pdf that references an existing PDF document.
  • The appendPages method is used to add pages from a PDF to the end of the referenced PDF.
  • To save the changes, the saveAndClose method is used.

Want to learn more?

If you’d like to learn more about working with the ArcGIS Pro Python window or migrating Python scripts from ArcMap to ArcGIS Pro, check out these resources:

If you'd like an introduction to ArcGIS Pro, these courses are recommended:

Esri instructor Brittney White This post was contributed by Esri instructor Brittney White. Based in Washington, D.C., Brittney’s passion is helping customers learn about and apply Esri technology to their own work.