Have you been meaning to try out the ArcMap Python window, but just aren’t sure how to start? Well, the five code snippets below are a great tool to familiarize yourself with the Python window and the arcpy.mapping module. Make a copy of an MXD file and move it somewhere to test out these snippets. You’ll soon see how useful a few Python tricks can be for map management tasks.
You don’t have to know any Python syntax to use these snippets. Simply enter the code into the Python window and check out the results.
Snippet 1: Check Layer Definition Queries
Suppose you need to check if any of the layers in your map document have a definition query applied, and if so, view the definition queries. Opening the Layer Properties dialog box for each layer is a pain.
Solution: Open the Python window instead and run the code below. The name and definition query of all the map layers will be “printed” (i.e., displayed in the Python window).
Code Breakdown
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?
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 map document 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).
Code Breakdown
Snippet 3: Identify Layers with a Broken Data Source
Speaking of those red exclamation marks, you can also use arcpy.mapping to print out the layers that have broken links using the code below.
Code Breakdown
Snippet 4: Inventory Layer Data Sources
If you need to know the data source for all the layers in your map, 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 the workspace path of the layers.
Code Breakdown
In this example, you print out the layer properties name and workspacePath for each layer in your map. Because not all layers have a workspace path (for example, web services), the supports method is used.
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 arcpy.mapping and the PDFDocument class to manipulate PDF documents. Run the code below to combine two PDF documents.
Code Breakdown
Want to learn more?
If you’d like to learn more about working with the ArcMap Python window or creating stand-alone Python scripts, these resources are recommended:
This post was contributed by Esri instructor @Brittney White.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.