Extract info from an mxd file programmatically

1069
9
04-05-2019 03:37 AM
LaurenceJackson
New Contributor

Hi All, some years ago one of our developers wrote a python script to identify text fields in an mxd file and extract information such as file names and layer names. The script doesn't work with recent versions/releases of ArcGIS. What is the best/official way to extract information from an mxd file programmatically on recent versions? ArcGIS API for Python? Or other API? Thanks

0 Kudos
9 Replies
JoshuaBixby
MVP Esteemed Contributor
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The ArcGIS API for Python is focused on web GIS, so it isn't designed to extract information out of MXDs.  In terms of Python, ArcPy would be the API/library you want to use to extract information out of MXDs.  You could also write some ArcObjects code (.NET, Java) to extract information as well.

My guess is that the script you have uses ArcPy, if it is a Python script for accessing information in MXDs.  What isn't working with it, specifically?

0 Kudos
MichaelVolz
Esteemed Contributor

Are you trying to use the old scripts in Pro to extract information from mxds that are part of ArcMap?

0 Kudos
LaurenceJackson
New Contributor

Thank you for your responses.

Joshua, the old scripts used to extract file paths, layer names, and CRS information. Only partial information is being returned now.

Michael, one of my colleagues tells me that you are probably right. They were old scripts in Pro reading ArcMap mxds. Sorry to be so vague but the scripts were written 7-8 years ago by a developer who left soon afterwards.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

ArcGIS Pro didn't exist 7-8 years ago, so these can't be "old scripts in Pro rereading ArcMap mxds."

The ArcPy mapping module was completely rewritten from ArcMap (arcpy.mapping) to Pro (arcpy.mp).  No arcpy.mapping code will run within the Pro application or using the Pro-bundled Python interpreter because the ArcMap version of the library isn't complied for Pro.

I am still not clear how you are trying to run these scripts.  But back to your original question, ArcPy is the best Python API for extracting information out of MXD or APRX files.

0 Kudos
curtvprice
MVP Esteemed Contributor

The arcpy.mapping Python module hasn't changed much since 10.1. I'm thinking this script just isn't handling edge cases the programmer didn't code for like grouped layers etc.

0 Kudos
LaurenceJackson
New Contributor

Thanks, everybody, for your responses.

Joshua, the scripts are being invoked by a Java application. I have looked at a couple of the scripts. The scripts start with an 'import arcpy.mapping' statement. Can I replace this statement with 'import arcpy.mp' and run the scripts against Pro projects?

0 Kudos
Egge-JanPollé1
MVP Regular Contributor

Hi Laurence Jackson

Please have a look at this Migrating from arcpy.mapping to ArcGIS Pro—ArcPy | ArcGIS Desktop for more information on the migration from arcpy.mapping to arcpy.mp. I don't think a simple find and replace will do...

Egge-Jan 

0 Kudos
curtvprice
MVP Esteemed Contributor

arcpy.mp will definitely not read MXDs. You need to install ArcMap and have your Java script crank up python from the ArcMap Python 2.7 executable. (For example, C:\Python27\ArcGIS10.6\python.exe.)  I'm pretty sure your code will all work if you use this Python stack, arcpy.mapping has not had big changes for a while. Most users still need both and ArcMap is not going away until 2025 so IMHO a full desktop install still really includes both.

Now, if you want your script to extract paths from ArcGIS Pro projects, that will require recoding using arcpy.mp and running the python script using the Pro python environment. (C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe). The structure is different so some software development will be needed.

0 Kudos