{"id":113412,"date":"2019-02-06T15:45:38","date_gmt":"2019-02-06T23:45:38","guid":{"rendered":"https:\/\/www.esri.com\/about\/newsroom\/?post_type=arcuser&#038;p=113412"},"modified":"2024-08-22T14:56:49","modified_gmt":"2024-08-22T21:56:49","slug":"create-a-python-tool-that-summarizes-arcmap-layer-properties","status":"publish","type":"arcuser","link":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties","title":{"rendered":"Create a Python Tool That Summarizes ArcMap Layer Properties"},"author":1432,"featured_media":0,"menu_order":0,"template":"","format":"standard","meta":{"_acf_changed":false,"sync_status":"","episode_type":"","audio_file":"","podmotor_file_id":"","podmotor_episode_id":"","castos_file_data":"","cover_image":"","cover_image_id":"","duration":"","filesize":"","filesize_raw":"","date_recorded":"","explicit":"","block":"","itunes_episode_number":"","itunes_title":"","itunes_season_number":"","itunes_episode_type":"","_links_to":"","_links_to_target":""},"categories":[10712,10392,25022],"tags":[163382,28932,238461],"arcuser_issues":[101622],"class_list":["post-113412","arcuser","type-arcuser","status-publish","format-standard","hentry","category-arcgis-api-for-python","category-arcgis-online","category-hands-on","tag-arcgis-online","tag-gis-education","tag-learn-gis","arcuser_issues-winter-2015"],"acf":{"short_description":"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026","pdf":{"host_remotely":false,"file":"","file_url":""},"flexible_content":[{"acf_fc_layout":"sidebar","layout":"standard","image_reference":null,"image_reference_figure":"","spotlight_image":null,"section_title":"","spotlight_name":"","position":"Center","content":"<h2>What You Will Need<\/h2>\r\n<ul>\r\n \t<li>ArcGIS 10.2 for Desktop<\/li>\r\n \t<li>An Internet connection<\/li>\r\n \t<li>A text editor<\/li>\r\n \t<li>Microsoft Excel (optional)<\/li>\r\n \t<li><a href=\"https:\/\/www.esri.com\/about\/newsroom\/app\/uploads\/2018\/10\/create-a-python-tool.zip\" target=\"_blank\" rel=\"noopener\">Sample dataset and resources<\/a>\u00a0<span class=\"fileInfo\">[ZIP]<\/span><\/li>\r\n<\/ul>","snippet":""},{"acf_fc_layout":"image","image":113432,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"content","content":"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gathers information about the layers in those documents, and outputs that information as a comma-separated values (CSV) file that can be viewed as a table in Microsoft Excel or another program that can open CSV files.\r\n\r\nTo work this exercise, you should be familiar with authoring Python scripts that use the ArcPy module, using ArcGIS for Desktop (especially ArcToolbox), and know how to add tools from a script file. This exercise uses several Python packages: ArcPy (the ArcGIS site package), os (miscellaneous operating system interfaces), fnmatch (UNIX file name pattern matching), and csv (CSV file reading and writing)."},{"acf_fc_layout":"image","image":113442,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"content","content":"<h2>The Challenge<\/h2>\r\nThe goal of this exercise is to create a tool that automatically inventories the map layers in a single ArcMap document or several ArcMap documents contained in a folder and generates a CSV file listing the properties of those layers.\r\n<h2>The Goal<\/h2>\r\nTo accomplish the challenge, you will develop a Python script that is run as a Python toolbox in ArcMap or ArcCatalog. This script will have user inputs for a directory or an individual ArcMap document (MXD) to parse and generate output as a CSV file. The CSV file can be opened in Microsoft Excel or another program that can read CSV files and be viewed as a table. By creating this tool in a Python toolbox, any ArcGIS user can use ArcPy functionality without knowing Python."},{"acf_fc_layout":"image","image":113452,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"content","content":"<h2>The Solution<\/h2>\r\nAlthough this example will work with one ArcMap document, the Python script will be able to process all ArcMap documents in a directory and any subdirectories it contains.\r\n\r\nCreate a new Python script using a Python editor or by simply creating a new text file (.txt) and changing the file extension to .py.\u00a0<em>[Note: If file extensions are not displayed in Windows, uncheck the Hide extensions for known file types in Folder Options located on Control Panel in the Windows operating system.]<\/em>\r\n\r\nThere are a variety of methods for developing this script. Listing 1 provides one example. It imports modules, defines the ArcMap documents that will be processed, defines their paths, parses the documents, and writes the information to a user-defined CSV file."},{"acf_fc_layout":"sidebar","layout":"code_snippet","image_reference":null,"image_reference_figure":"","spotlight_image":null,"section_title":"","spotlight_name":"","position":"Center","content":"(This first section imports the modules.)","snippet":"#Import modules...\r\nimport arcpy, os, fnmatch, csv"},{"acf_fc_layout":"sidebar","layout":"code_snippet","image_reference":null,"image_reference_figure":"","spotlight_image":null,"section_title":"","spotlight_name":"","position":"Center","content":"(The next section defines the user input variables.)","snippet":"#User input variables...\r\nmxddirectory = arcpy.GetParameterAsText(0)\r\nmxd_single = arcpy.GetParameterAsText(1)\r\noutputcsvlocation = arcpy.GetParameterAsText(2)"},{"acf_fc_layout":"sidebar","layout":"code_snippet","image_reference":null,"image_reference_figure":"","spotlight_image":null,"section_title":"","spotlight_name":"","position":"Center","content":"(This section defines the ArcMap documents to parse.)","snippet":"#Create an empty list of ArcMap documents to process...\r\nmxd_list=[]\r\n#If a user defined a single mxd, add its path to the list...\r\nif len(mxd_single) &gt; 0:\r\n  mxd_list.append(mxd_single)\r\n#Otherwise walk through the input directory, adding paths for\r\neach .mxd file found to the list...\r\nelse:\r\n  for dirpath in os.walk(mxddirectory): #os.walk returns \\\r\n    (dirpath, dirnames, filenames)\r\n    for filename in dirpath[2]:\r\n      if fnmatch.fnmatch(filename, \"*.mxd\"):\r\n        mxd_list.append(os.path.join(dirpath[0], filename))"},{"acf_fc_layout":"sidebar","layout":"code_snippet","image_reference":null,"image_reference_figure":"","spotlight_image":null,"section_title":"","spotlight_name":"","position":"Center","content":"(This section parses the documents and writes the information to a user-defined CSV file.)","snippet":"#Iterate the list of mxd paths and gather property values then\r\nwrite to csv file...\r\nif len(mxd_list) &gt; 0:\r\n  #Create the csv file...\r\n  outputcsv = open(outputcsvlocation,\"wb\")\r\n  writer = csv.writer(outputcsv, dialect = 'excel')\r\n  #Write a header row to the csv file...\r\n  writer.writerow([\"mxdpath\", \"layername\", \"layerdescription\", \"layersource\"])\r\n  #Iterate through the list of ArcMap Documents...\r\n  for mxdpath in mxd_list:\r\n    mxdname = os.path.split(mxdpath)[1]\r\n    try:\r\n      mxd = arcpy.mapping.MapDocument(mxdpath)\r\n      #Iterate through the ArcMap Document layers...\r\n      for layer in arcpy.mapping.ListLayers(mxd):\r\n        layerattributes = [mxdpath, layer.longName, \\\r\n        layer.description, layer.dataSource]\r\n        #Write the attributes to the csv file...\r\n        writer.writerow(layerattributes)\r\n    except:\r\n      arcpy.AddMessage(\"EXCEPTION: {0}\".format(mxdpath))\r\n    del mxd\r\n  #close the csv file to save it...\r\n  outputcsv.close()\r\n#If no ArcMap Documents are in the list, then notify via an \r\nerror message...\r\nelse:\r\n  arcpy.AddError(\"No ArcMap Documents found. Please check your input \\\r\n    variables.\")"},{"acf_fc_layout":"image","image":113492,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"content","content":"<h2>Adding the Script to a Toolbox<\/h2>\r\nOpen ArcMap, then open Catalog. In Catalog, navigate to the folder where you want to keep the new toolbox for the script and right-click it. Choose New &gt; Toolbox. Rename the toolbox.\r\n\r\nRight-click New toolbox and choose Add &gt; Script. In the Add Script dialog box, enter name (ArcMapDocumentPropertiesToCSV), label (ArcMapDocumentPropertiesToCSV), and description (Parses through one or many ArcMap documents and collects property values into a CSV file) for the script. Check the box next to Enable the Store relative path names to eliminate the need to update the toolbox on the script's source path. Click Next.\r\n\r\nUse the Browse button to select the script file. Leave Show command window when executing script box unchecked because the script itself includes the ability to display messages in the Geoprocessing window. Check Run Python script in process to allow it to run faster.\r\n\r\nIn the next pane, add the user input variables.\r\n\r\nIn the upper grid, fill in the values in Table 1."},{"acf_fc_layout":"image","image":113522,"image_position":"left-center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"image","image":113532,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"image","image":113542,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"image","image":113562,"image_position":"center","orientation":"horizontal","hyperlink":""},{"acf_fc_layout":"content","content":"<h2>Running the Tool<\/h2>\r\nDouble-click the script tool to invoke it. In the dialog box, click the first Browse button to select a folder directory (mxd_directory) or the second Browse button to select a single ArcMap document.\r\n\r\nIf parameters are entered for both, the single ArcMap document will be parsed and not the ArcMap documents in the folder directory specified. If no input is given, then an error will occur. Select an output file and directory. It is best to use the wizard for each user input variable.\r\n\r\nThis script can be expanded to include reading other properties for the MXD and its layers. It can also be used to set the values for certain properties. For example, dates, URLs, and organization names could be updated.\r\n<h2>Summary and Conclusion<\/h2>\r\nPython's csv module and ArcPy have made it an easier task to gather information about ArcMap documents and the layers they contain and summarize that information in a format that can be viewed as a standard spreadsheet. Authoring the Python script so it can be used as a script in an Esri toolbox means processing can be used repeatedly by others who may not know how to code in Python or ArcPy.\r\n\r\nFor more information, contact\u00a0<a href=\"mailto:clindeman@pdc.org\">Colin Lindeman<\/a>."},{"acf_fc_layout":"pdf","file":113572}],"references":null},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Create a Python Tool That Summarizes ArcMap Layer Properties | Winter 2015 | ArcUser<\/title>\n<meta name=\"description\" content=\"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create a Python Tool That Summarizes ArcMap Layer Properties\" \/>\n<meta property=\"og:description\" content=\"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties\" \/>\n<meta property=\"og:site_name\" content=\"Esri\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/esrigis\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-22T21:56:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.esri.com\/about\/newsroom\/app\/uploads\/2019\/09\/topography-dark-grey-card.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@Esri\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\/\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties\",\n\t            \"url\": \"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties\",\n\t            \"name\": \"Create a Python Tool That Summarizes ArcMap Layer Properties | Winter 2015 | ArcUser\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/#website\"\n\t            },\n\t            \"datePublished\": \"2019-02-06T23:45:38+00:00\",\n\t            \"dateModified\": \"2024-08-22T21:56:49+00:00\",\n\t            \"description\": \"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026\",\n\t            \"breadcrumb\": {\n\t                \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties#breadcrumb\"\n\t            },\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"BreadcrumbList\",\n\t            \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties#breadcrumb\",\n\t            \"itemListElement\": [\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 1,\n\t                    \"name\": \"Home\",\n\t                    \"item\": \"https:\/\/www.esri.com\/about\/newsroom\"\n\t                },\n\t                {\n\t                    \"@type\": \"ListItem\",\n\t                    \"position\": 2,\n\t                    \"name\": \"Create a Python Tool That Summarizes ArcMap Layer Properties\"\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/#website\",\n\t            \"url\": \"https:\/\/www.esri.com\/about\/newsroom\/\",\n\t            \"name\": \"Esri\",\n\t            \"description\": \"Esri Newsroom\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\/\/www.esri.com\/about\/newsroom\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/#\/schema\/person\/b7332d419608ac1a0291ec30de119efb\",\n\t            \"name\": \"April Mann\",\n\t            \"image\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\/\/www.esri.com\/about\/newsroom\/#\/schema\/person\/image\/\",\n\t                \"url\": \"https:\/\/secure.gravatar.com\/avatar\/28d21cf04453f95cd2d8a0f053e5cd18ccf684320f6a4974f358b8109264e80b?s=96&d=blank&r=g\",\n\t                \"contentUrl\": \"https:\/\/secure.gravatar.com\/avatar\/28d21cf04453f95cd2d8a0f053e5cd18ccf684320f6a4974f358b8109264e80b?s=96&d=blank&r=g\",\n\t                \"caption\": \"April Mann\"\n\t            },\n\t            \"url\": \"\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Create a Python Tool That Summarizes ArcMap Layer Properties | Winter 2015 | ArcUser","description":"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties","og_locale":"en_US","og_type":"article","og_title":"Create a Python Tool That Summarizes ArcMap Layer Properties","og_description":"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026","og_url":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties","og_site_name":"Esri","article_publisher":"https:\/\/www.facebook.com\/esrigis\/","article_modified_time":"2024-08-22T21:56:49+00:00","og_image":[{"url":"https:\/\/www.esri.com\/about\/newsroom\/app\/uploads\/2019\/09\/topography-dark-grey-card.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@Esri","twitter_misc":{"Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties","url":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties","name":"Create a Python Tool That Summarizes ArcMap Layer Properties | Winter 2015 | ArcUser","isPartOf":{"@id":"https:\/\/www.esri.com\/about\/newsroom\/#website"},"datePublished":"2019-02-06T23:45:38+00:00","dateModified":"2024-08-22T21:56:49+00:00","description":"This article shows how to create Python script that runs as a tool in ArcToolbox. This tool automatically batch processes ArcMap documents, gath\u2026","breadcrumb":{"@id":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.esri.com\/about\/newsroom\/arcuser\/create-a-python-tool-that-summarizes-arcmap-layer-properties#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.esri.com\/about\/newsroom"},{"@type":"ListItem","position":2,"name":"Create a Python Tool That Summarizes ArcMap Layer Properties"}]},{"@type":"WebSite","@id":"https:\/\/www.esri.com\/about\/newsroom\/#website","url":"https:\/\/www.esri.com\/about\/newsroom\/","name":"Esri","description":"Esri Newsroom","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.esri.com\/about\/newsroom\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.esri.com\/about\/newsroom\/#\/schema\/person\/b7332d419608ac1a0291ec30de119efb","name":"April Mann","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.esri.com\/about\/newsroom\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28d21cf04453f95cd2d8a0f053e5cd18ccf684320f6a4974f358b8109264e80b?s=96&d=blank&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28d21cf04453f95cd2d8a0f053e5cd18ccf684320f6a4974f358b8109264e80b?s=96&d=blank&r=g","caption":"April Mann"},"url":""}]}},"sort_order":"0","_links":{"self":[{"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/arcuser\/113412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/arcuser"}],"about":[{"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/types\/arcuser"}],"author":[{"embeddable":true,"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/users\/1432"}],"version-history":[{"count":0,"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/arcuser\/113412\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/media?parent=113412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/categories?post=113412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/tags?post=113412"},{"taxonomy":"arcuser_issues","embeddable":true,"href":"https:\/\/www.esri.com\/about\/newsroom\/wp-json\/wp\/v2\/arcuser_issues?post=113412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}