Select to view content in your preferred language

Create a .mxd project from scratch via arcpy.

341
3
10-25-2022 11:31 AM
PabloUrbinaTerán
New Contributor

I know that right now you can't create a .mxd from scratch in ArcPy through a command line, in Python 2. You can only add layers to a .mxd.

Is it possible to create a function where such a document can be created from scratch?

0 Kudos
3 Replies
RhettZufelt
MVP Notable Contributor

Does not appear to be.  

Have not tested it, but you might be able to make a "template" mxd that your script references, then use the 

saveACopy (file_name, {version})

to save to "new" mxd.

R_ 

DonMorrison1
Frequent Contributor

I have never found a way to create one from scratch so I have to have a "blank template" available to use as the starting point.  Something like this:

mxd_template_obj = arcpy.mapping.MapDocument(<blank_template_fn>)
df = arcpy.mapping.ListDataFrames(mxd_template_obj)[0] 
arcpy.mapping.AddLayer(df, <layer file name>, "BOTTOM")
mxd_template_obj.saveACopy(<mxd file name>)

  

Luke_Pinner
MVP Regular Contributor

Apart from the blank template method, this is not possible using arcpy. This is an intentional design decision:

Must work with existing map documents or layer files
The arcpy.mapping module was designed so that it can be used to modify existing elements within already existing map documents (.mxd) or layer files (.lyr).  <...snip...> You must carefully author a map document or layer file using ArcMap ahead of time with all the appropriate elements and then use arcpy.mapping to manipulate its contents

However, it was possible with python by accessing ArcObjects. For more details, see this answer on GIS StackExchange.  This was written quite a long time ago, I don't know if it still works.

0 Kudos