Read MXD path

788
3
04-12-2013 07:20 AM
EduardoCamargo_de_Araujo
New Contributor
Hy Guys,

I'm new in python and I would like to know if there is a way to read the MXD path (e.g C:\temp\arquivo.mxd) ?

I have to create a MXD, then open it and add some layers, I tried this:
import arcpy, os

mxd = file("template1.mxd", "a")
mxd.close()
mxd1 = arcpy.mapping.MapDocument(mxd)
df = arcpy.mapping.ListDataFrames(mxd1, "Dados")[0]
addLayer = arcpy.mapping.Layer(r"D:\Eduardo\Dados_teste\Temp\sanepar\TemplateFlags.lyr")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd1.save()
del mxd1, addLayer

It works if the MXD is previously created but I'd like to create a blank MXD.  I don't know how to pass the mxd full path to arcpy.mapping.MapDocument.

Thanks,
Eduardo
Tags (2)
0 Kudos
3 Replies
RobertBorchert
Frequent Contributor III
Do you mean the path to where the MXD is or a path to where the data associated with the MXD is.

If you mean where the data associated with the MXD is.

Start ArcCatalog.

Navigate to where your MXD is.  Right Click on it and select Set Data Sources.

Or inside or an ArcGIS session In the table of contents there should be a button to list by source.  And it will show you all the sources.


Hy Guys,

I'm new in python and I would like to know if there is a way to read the MXD path (e.g C:\temp\arquivo.mxd) ?

I have to create a MXD, then open it and add some layers, I tried this:
import arcpy, os

mxd = file("template1.mxd", "a")
mxd.close()
mxd1 = arcpy.mapping.MapDocument(mxd)
df = arcpy.mapping.ListDataFrames(mxd1, "Dados")[0]
addLayer = arcpy.mapping.Layer(r"D:\Eduardo\Dados_teste\Temp\sanepar\TemplateFlags.lyr")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd1.save()
del mxd1, addLayer

It works if the MXD is previously created but I'd like to create a blank MXD.  I don't know how to pass the mxd full path to arcpy.mapping.MapDocument.

Thanks,
Eduardo
0 Kudos
EduardoCamargo_de_Araujo
New Contributor
Thanks Robert,

but I would like to know Where the MXD is (e.g C:\temp\test.mxd) !

Using the ArcMap interface I know how to get this. I would like to do this through python.
0 Kudos
JeffMoulds
Esri Contributor
If you want to read the filePath property of the MapDocument, you can do this:
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
print mxd.filePath


However, from your previous thread, it sounds like you also might want to create a new blank mxd in your script. This is not possible. The arcpy.mapping module was designed so that it can be used to modify existing elements within already existing map documents. In other words, it helps with the automation of existing features but it can't be used to author new objects. It is possible to make changes to existing map documents and then save the changes out to a new file using the saveACopy method on the MapDocument or Layer objects. It also possible to author and save a blank mxd in the UI, and then reference it with arcpy.mapping.MapDocument.
0 Kudos