Conversion of MXDs to APRX Using Python

900
11
01-07-2024 09:30 PM
naveenbesri
Occasional Contributor

Hi All,

I have been using python script for conversion of MXDs to APRX files. we are able to create the aprx files but when I open its empty no layers or properties in newly converted aprx file. below is the script

 

import os
import pathlib
import arcpy

ogDir = r'D:\MXDs'
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('Current')
filePath = pathlib.Path(folder_path) / file
mxd = file[:-4]
aprx.importDocument(filePath)
aprx.saveACopy(folder_path + '\\' + mxd + '.aprx')

 

Kindly help me out where I can get same as like my MXD into aprx file. 

 

Thanks,

Naveen

0 Kudos
11 Replies
CedricDespierreCorporon
Esri Contributor

Hello,

In the documentation examples it's noted that the use of the "current" keyword can only works within the arcgis pro python windows.

ArcGISProject—ArcGIS Pro | Documentation

To make it work, you need to create manually a blank/empty project and open it explicitly :

aprx = arcpy.mp.ArcGISProject(r"C:\Projects\blank.aprx")

0 Kudos
naveenbesri
Occasional Contributor

Hi @CedricDespierreCorporon  I have created blank aprx file and passed the this as parameter. we are getting the below error. 

Traceback (most recent call last):
File "<string>", line 13, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 409, in __init__
self._arc_object = arcgisscripting._mapping.ArcGISProject(*gp_fixargs((aprx_path,), True))
OSError: D:\mxd_test\blank.aprx

0 Kudos
CedricDespierreCorporon
Esri Contributor

Hello,

usually, OS errors could be related to 3 things :

  • error in the path
  • Python user executing the scripts have no rights (file access/read/write) on D:\mxd_test\blank.aprx
  • aprx created on arcgis pro 3.x+ and the machine with the python script is running 2.x

 

 

SYOCSan
New Contributor II

My code looks the same as yours and works. 

The only difference is where your code reads

aprx = arcpy.mp.ArcGISProject('Current')

mine reads

aprx = arcpy.mp.ArcGISProject(r'C:\Users\NAME\Documents\ArcGIS\Projects\MyProjectTest\MyProjectTest.aprx')

I opened a project , saved it, and added the script to run from the toolbox.

If you go to Project Tab > Info there is an easy copy path button for pasting your project name

Also, before hand I went to the Project Tab > Package manager and cloned my environment and updated all my packages

I am running on Pro 3.1.3

Python-2.0

 

0 Kudos
naveenbesri
Occasional Contributor

Hi, I have changed the code as suggested by you created new blank project and passed aprx as parameter to ArcGISProject by removing current. Script ran successfully but the same issue as mentioned above getting same empty aprx file. 

naveenbesri_0-1705299645051.png

 

0 Kudos
CedricDespierreCorporon
Esri Contributor

Hello @naveenbesri : the code should create a new map within the project with the MXD layers, and it will not add them to the default opened map. Checks the list of maps it should be there.

0 Kudos
naveenbesri
Occasional Contributor

Hi, I did the same. Mxds got converted to aprx in the mentioned folder and after that when I open I saw no layers added which I have posted above. Kindly help

0 Kudos
naveenbesri
Occasional Contributor

I ran the python script in python window. below is the screenshot

naveenbesri_0-1705383760165.png

and after that aprx files have been created but no data in it. 

naveenbesri_1-1705383824302.png

 

0 Kudos
CedricDespierreCorporon
Esri Contributor

Hello, the file size of each aprx is different so they should not be empty.

CedricDespierreCorporon_0-1705396497683.png

Imported MXD should be present in a "new" map within the project : you can have multiple maps in one project with arcgis pro.

CedricDespierreCorporon_1-1705396655340.png

 

 

0 Kudos