importDocument() OSError

888
6
Jump to solution
11-25-2019 12:00 PM
JaredPilbeam2
MVP Regular Contributor

I'm attempting to convert MXDs to APRXs using the MXD filenames in a stand alone script. I'm basically using this code from a prior post: How to assign name to map layer in APRX when importing from MXD? 

I have a test folder with six MXDs in it. The script gets as far as beginning to loop through the folder but then throws an OSError on the first MXD. The error is pointing towards the importDocument line (line 14 below).

import arcpy

#List MXDs in workspaceMXD
workspaceMXD = r"C:\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder"
arcpy.env.workspace = workspaceMXD
arcpy.env.overwriteOutput = True
mxdlist = arcpy.ListFiles("*.mxd")

#aprx project
aprx = arcpy.mp.ArcGISProject(r"C:\GISmaps\AtlasMaps\MXDtoAPRX_tempdelete\MXDtoAPRX_tempdelete.aprx")

for mxd in mxdlist:
    print("Importing: {0}".format(mxd.split("\\")[-1]))
    aprx.importDocument(mxd,include_layout=True)

    #the count of the maps
    print("Currently {0} maps in Project file.".format(len(aprx.listMaps())))

    #name the maps
    aprx.listMaps()[-1].name = mxd.split("\\")[-1][:-4]

aprx.saveACopy(r"C:\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Error

Importing: Beecher_Unit_200U_B.mxd
Traceback (most recent call last):
  File "C:\GISstaff\Jared\Python Scripts\ArcGISPro\MxdToAprx.py", line 14, in <module>
    aprx.importDocument(mxd,include_layout=False)
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 223, in importDocument
    return convertArcObjectToPythonObject(self._arc_object.importDocument(*gp_fixargs((document_path, include_layout, reuse_existing_maps), True)))
OSError: Beecher_Unit_200U_B.mxd‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

If I import one MXD at a time and save it as an APRX as seen in the code sample on the the ArcGISProject page, it works fine. But, I'm working with multiple MXDs. Does anyone know what this error is referring to?

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

I know you set the workspace and split the mxd name from the path apparently, 

but it may want the fully qualified path and mxd name

document_path

also, try skipping the layout thing, True is the default anyway

importDocument (document_path, {include_layout}, {reuse_existing_maps})

View solution in original post

0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I know you set the workspace and split the mxd name from the path apparently, 

but it may want the fully qualified path and mxd name

document_path

also, try skipping the layout thing, True is the default anyway

importDocument (document_path, {include_layout}, {reuse_existing_maps})

0 Kudos
JaredPilbeam2
MVP Regular Contributor

Dan,

I set it up with the full path and did not include the Layout parameter, but it's giving me the same error.

for mxd in mxdlist:
    fullpath = os.path.join(workspaceMXD, mxd)
    print("Importing: {}".format(fullpath))
    aprx.importDocument(mxd)
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Oops, I forgot to add the fullpath variable to the importDocument function. So, I did that just now and it worked:

aprx.importDocument(fullpath)

The problem is now it's saving all the maps in one project instead of 6 individual ones? I put aprx.saveACopy in the for loop and I left it out of the for loop, but same difference?

0 Kudos
deleted-user-NvcfpBOWaKwr
Occasional Contributor

Try giving the saveACopy a full path, file name and extension and see what it does.

I had to feed in a full path along with the new name and file extension for mxd.saveACopy() in one of my scripts. Hopefully this is the same for aprx.saveACopy() and it starts working as intended.

Below is an example:

aprx.saveACopy(r"C:\GISmaps\AtlasMaps\ATLAS_MAPS_20\TestFolder\Documents_New_Name.aprx")
DanPatterson_Retired
MVP Esteemed Contributor

It seems a lot of things in the mp don't support many if any "Environments" as is commonly found in other arcpy classes, methods etc.

Full path specification seems to be the standard, and hence importDocument needs full paths.

ArcGISProject—ArcPy | ArcGIS Desktop the project itself doesn't list any supported Environments at the bottom of the help topic.

"Current" seems to be the only project that allows for simplification of file paths/data sources.

JaredPilbeam2
MVP Regular Contributor

@jeremy If I give it a full path, file name and extension it will save one APRX with that file name.

The best results I can get so far is by putting the fullpath variable in the saveACopy() function. This saves the six APRX files individually in the folder. However, now some of the maps are imported twice? For example:

Since my original question was already answered by Dan, I'll go ahead and mark it as such. Thanks for the help.

0 Kudos