add one feature class to one mxd arcpy

282
8
08-24-2018 04:12 PM
LETITIAKING-BRANCH
New Contributor II

Hello

I'm trying to create one mxd for each for layer from an existing MXD.  I have list of layers in a mxd but  I want  one mxd to one layer.  This is the script I've been using. It copies all of the layers to the new mxd.   

import arcpy
... mxd = arcpy.mapping.MapDocument("CURRENT")
... for df in arcpy.mapping.ListLayers(mxd):

          df.name
          mxd.saveACopy(r"C:\Project\Output\\" + df.name + ".mxd")
... del mxd

0 Kudos
8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

All the layers in the dataframe will be there by default.  In order to do what you want to do, you would have to add a layer, save it out, remove the layer, add a new one and repeat.

What is the purpose of having one mxd that only contains 1 layer? Perhaps other solutions can be provided that would meet your goals

0 Kudos
LETITIAKING-BRANCH
New Contributor II

The purpose of having one mxd for one layer is to create a service for each route layer for ArcGIS online.  This will used to display each individual bus route.  Thanks

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Ahhhh… I will share to ArcGIS Online‌ … it didn't quite make sense for just working with arcmap or Pro unless you were making personalized lab assignments (each student... different data)

JoshuaBixby
MVP Esteemed Contributor

You would need to create an empty MXD to use as a template for inserting each layer into a new, empty map document:

import arcpy

mxd_empty = # path to empty mxd

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    mxde = arcpy.mapping.MapDocument(mxd_empty)
    dfe = arcpy.mapping.ListDataFrames(mxde)[0]
    arcpy.mapping.AddLayer(dfe, lyr)
    mxde.saveACopy(r"C:\Project\Output\\" + lyr.name + ".mxd")

del lyr, mxd, dfe, mxde
LETITIAKING-BRANCH
New Contributor II

I'm getting runtime error Invalid mxd filename

>>> import arcpy
...
... mxd_empty = "c:\project\Output"
...
... mxd = arcpy.mapping.MapDocument("CURRENT")
... for lyr in arcpy.mapping.ListLayers(mxd):
...     mxde = arcpy.mapping.MapDocument(mxd_empty)
...     dfe = arcpy.mapping.ListDataFrames(mxde)[0]
...     arcpy.mapping.AddLayer(dfe, lyr)
...     mxde.saveACopy(r"C:\Project\Output\\" + lyr.name + ".mxd")
...
... del lyr, mxd, dfe, mxde
...
Runtime error
Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\arcobjects\mixins.py", line 651, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you see to be mixing raw and double backslash formatting.

You might want to print the layer name prior to concatenating to see what it is showing

r"C:\Project\Output\\"   should be r"C:\Project\Output\" 

0 Kudos
LETITIAKING-BRANCH
New Contributor II

Now I'm getting

 >>> import arcpy
...
... mxd_empty = "C:\Project\Output"
...
... mxd = arcpy.mapping.MapDocument("CURRENT")
... for lyr in arcpy.mapping.ListLayers(mxd):
...     mxde = arcpy.mapping.MapDocument(mxd_empty) 
...     dfe = arcpy.mapping.ListDataFrames(mxde)[0]
...     arcpy.mapping.AddLayer(dfe, lyr)
...     mxde.saveACopy(r"C:\Project\Output\" + lyr.name + ".mxd")
...
... del lyr, mxd, dfe, mxde
...
Parsing error SyntaxError: EOL while scanning string literal (line 10)

0 Kudos
LETITIAKING-BRANCH
New Contributor II

  I had to include the actually empty. mxd in the path not just the directory.  Thanks Josh

import arcpy
...
... mxd_empty = "c:\project\output\empty.mxd"
...
... mxd = arcpy.mapping.MapDocument("CURRENT")
... for lyr in arcpy.mapping.ListLayers(mxd):
...     mxde = arcpy.mapping.MapDocument(mxd_empty)
...     dfe = arcpy.mapping.ListDataFrames(mxde)[0]
...     arcpy.mapping.AddLayer(dfe, lyr)
...     mxde.saveACopy(r"C:\Project\Output\\" + lyr.name + ".mxd")
...
... del lyr, mxd, dfe, mxde
...

0 Kudos