Select to view content in your preferred language

Batch export of mxd to aprx without without data stored in gdb using python

462
2
Jump to solution
01-31-2025 06:32 AM
Labels (1)
Paul-EmmanuelLeroux
Emerging Contributor

Hello ESRI community.

We ar willing to migrate or work from ArcMap to ArcGIS Pro. We have over 200 mxd we need to export to APRX. We are not motivated to do that manualy. We would rather use a python script to do so. 

We got this in the web how ever, it doesn't work. My colleague and I are not python user so it's complcated.

Moreover we want that the mxd data not to stored into the gdb of the project but to the shapefile. All the MXD we have, have only 5 common shapefiles data on a specific folder. we want to keep the data sources where they currently are. 

here is the script we started: 

import arcpy
import pathlib
import os
 
ogDir =input('path to mxd folder')
dirContents = os.listdir(ogDir)
 
for folder_path, subfolders, filenames in os.walk(ogDir):
    for file in filenames:
        if file[-4:] == '.mxd':
            aprx = arcpy.mp.ArcGISProject(r'path to blank.aprx')
    filePath = pathlib.Path(folder_path) / file
            mxd = file[:-4]
            aprx.importDocument(filePath)
            aprx.saveACopy(folder_path + '\\' + mxd + '.aprx')
 
after mxd=file[:-4] i have the following comment:
 
IndentationError: unexpected indent
then
IndentationError: unexpected indent
>>> aprx.importDocument(filePath)
File "<stdin>", line 1
aprx.importDocument(filePath)
IndentationError: unexpected indent
>>> aprx.saveACopy(folder_path + '\\' + mxd + '.aprx')
 
If you have bette script I would take it.
 
Final question car we run the script here?
 
PaulEmmanuelLeroux_0-1738333929900.png

 

 

0 Kudos
1 Solution

Accepted Solutions
Marshal
Frequent Contributor

I don't believe .importDocument() moves or copies data sources.  So if your MXD references shapefiles, the imported APRX map will reference the same data sources.

Yes, you can use propy.bat to execute the python script.

As far as the script itself, I would do something like below (not tested though).

 

import arcpy
import os

# User input for MXD directory
in_dir = r'C:\Path\To\MXDs'  # Update this path

# Validate if the input directory exists
if not os.path.exists(in_dir):
    print('Error: The specified directory does not exist.')
    exit()

# Path to a blank ArcGIS Pro project (.aprx)
blank_aprx_path = r'C:\Path\To\Blank.aprx'  # Update this path

# Using arcpy.da.Walk to traverse the directory for MXD files
for dirpath, dirnames, filenames in arcpy.da.Walk(in_dir, datatype='Map'):
    for file in filenames:
        if file.lower().endswith('.mxd'):  # Filter only .mxd files
            file_path = os.path.join(dirpath, file)  # Full path to MXD
            mxd_name = os.path.splitext(file)[0]  # Remove .mxd extension

            try:
                # Create a ArcGISProject object from the blank template
                aprx = arcpy.mp.ArcGISProject(blank_aprx_path)

                # Import the MXD document
                aprx.importDocument(file_path)

                # Save a copy of the new APRX in the same folder
                aprx.saveACopy(os.path.join(dirpath, f'{mxd_name}.aprx'))

                print(f'Successfully converted: {file_path}')

            except Exception as e:
                print(f'Error converting {file_path}: {e}')

 

 

View solution in original post

0 Kudos
2 Replies
Marshal
Frequent Contributor

I don't believe .importDocument() moves or copies data sources.  So if your MXD references shapefiles, the imported APRX map will reference the same data sources.

Yes, you can use propy.bat to execute the python script.

As far as the script itself, I would do something like below (not tested though).

 

import arcpy
import os

# User input for MXD directory
in_dir = r'C:\Path\To\MXDs'  # Update this path

# Validate if the input directory exists
if not os.path.exists(in_dir):
    print('Error: The specified directory does not exist.')
    exit()

# Path to a blank ArcGIS Pro project (.aprx)
blank_aprx_path = r'C:\Path\To\Blank.aprx'  # Update this path

# Using arcpy.da.Walk to traverse the directory for MXD files
for dirpath, dirnames, filenames in arcpy.da.Walk(in_dir, datatype='Map'):
    for file in filenames:
        if file.lower().endswith('.mxd'):  # Filter only .mxd files
            file_path = os.path.join(dirpath, file)  # Full path to MXD
            mxd_name = os.path.splitext(file)[0]  # Remove .mxd extension

            try:
                # Create a ArcGISProject object from the blank template
                aprx = arcpy.mp.ArcGISProject(blank_aprx_path)

                # Import the MXD document
                aprx.importDocument(file_path)

                # Save a copy of the new APRX in the same folder
                aprx.saveACopy(os.path.join(dirpath, f'{mxd_name}.aprx'))

                print(f'Successfully converted: {file_path}')

            except Exception as e:
                print(f'Error converting {file_path}: {e}')

 

 

0 Kudos
Paul-EmmanuelLeroux
Emerging Contributor

Hello Marshal,

Thanks you very much the script works well in our test phase.

Best regards,

 

Paul-Emmanuel

 

 

Best regards,

 

Paulo