The method of importing an MXD to an ArcGIS Pro project has already been asked as Importing MXD to ArcGIS Pro programmatically? and it goes like this:

projectsArea = r'C:ArcGIS\Projects'
APRX_name = 'ArcGIS Project.aprx'
output_APRX = os.path.join(projectsArea, APRX_name)

# Variables
arcpy.env.overwriteOutput = True

aprx = arcpy.mp.ArcGISProject("Current")
arcpy.env.workspace = workspaceMXD

# List files in workspaceMXD
listFiles = arcpy.ListFiles("*.mxd")

# Import MXD Files
for files in listFiles:
  # file names in list appended to folder path
  inputMXD = os.path.join(workspaceMXD, files)
  print (inputMXD)

This results in a folder of MXD's being imported into an ArcGIS Pro Project - however, these imported maps are named according to their data frame name (e.g. Layers).

How can you access the MXD filename and include it as a variable in the aprx.importDocument class?


1 Answer 1


The short answer is: "you can't reliably do this with the functions that are available."

The longer answer is: "you can do this if you allow your code to make some assumptions".

The ArcGISProject class as you point out allows you to import a MXD. The Map class allows you to read and write the map name.

The following code, based on your starting code will:

  • Find all MXDs in a folder
  • import each MXD into the project
  • Immediately after importing the MXD it'll:
  • Assume the map that has been loaded was called Layers

  • Change the name of that map to the file name of the MXD.

If the Dataframe in the MXD was NOT "Layers", it will NOT rename the newly imported map, and the code will bail out when trying to rename the map. You could try/except it, but now you run the risk that the code will identify the wrong map as you'll have 2 or more maps that have not been renamed. The logic (and flaw) of this code is it assumes there is only 1 map in the project named Layers and assumes it can rename that map to the MXD file name.

import os
mxdDir =r"C:\data\mxds"
arcpy.env.workspace = mxdDir
mxds = arcpy.ListFiles("*.mxd")
> ['mxd1.mxd', 'mxd22.mxd', 'mxd33.mxd']
prj = arcpy.mp.ArcGISProject("CURRENT")
for m in mxds:
    print("Importing {} ".format(m))
    prj.importDocument(os.path.join(mxdDir, m))
    pMap = prj.listMaps("Layers")[0]
    pMap.name = m.split(".mxd")[0]
> Importing mxd1.mxd
> Importing mxd22.mxd
> Importing mxd333.mxd
  • Exactly what I was looking for. Thanks
    – Dowlers
    Oct 1, 2021 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.