Provide ability to create new ArcGIS Pro projects from Python

3254
18
09-01-2021 09:54 AM
MaryGraceMcClellan
New Contributor III

I'm creating a script that will convert all of my MXDs to APRXs, and I find it extremely inconvenient that I can't create a new blank project from scratch form within ArcGIS Pro. This utility would make switching to ArcGIS Pro so much easier, along with the ability to create blank toolboxes. 

Edited to add: @Rhys-Donoghue posted a good workaround for this issue if anyone is interested. 

18 Comments
GraemeBrowning
I know that arcpy.mapping was not designed to author new objects - http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s30000006s000000 - but I wish that it had been and would like to see ArcPy enhanced to be able to create a new map document.
ChrisFox
What if you had a blank MXD available with the tool or script that you just created a copy from and did what you needed to do?
GraemeBrowning
If that blank MXD was part of the ArcGIS install then I would consider it a solution but if you mean post-install having to ensure that one is made available on every machine my code gets run on then to me that's a workaround.

Its a shame the code below does not work - perhaps arcpy.mapping.MapDocument() can be enhanced to allow opening of the Normal.mxt?

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Users\Graeme\AppData\Roaming\ESRI\Desktop10.1\ArcMap\Templates\Normal.mxt")
mxd.saveACopy(r"C:\temp\test.mxd")

ChrisFox
At 10.0 and 10.1 you will find many MXDs are available with the install at:

<ArcGIS Desktop Install>\MapTemplates

This gives you all sorts of MXDs with different layout sizes and orientation. In addition, depending on how you are sharing your code, if you are building a python add-in it is possible to package data including a blank MXD along with your add-in:

http://resources.arcgis.com/en/help/main/10.1/#/Essential_Python_add_in_concepts/014p0000001p000000/
GraemeBrowning
Thanks Chris

Those two recommendations should provide good workarounds (even a virtual solution).

However, it will probably just shift the blocking point for me to being why can't new layout elements and other objects like grids and graticules be created using ArcPy?
ChrisFox
Yes, unfortunately those are just not implemented. We did add the ability at 10.1 to clone a text or graphic element in the layout. So if one already exists you can clone a copy it and modify some properties.
GraemeBrowning
Cloning text and graphic elements so properties can be modified is certainly a help so thanks for reminding me of them.

The biggest one for me is not being able to change intervals on existing graticules using ArcPy - hopefully the ArcGIS Idea related to that will continue to attract votes.
SachinKanaujia
A must have !!!
maxsquires2

sources:

arcpy - Seeking Python script for creating .mxd files? - Geographic Information Systems Stack Exchan... 

pip - Installing python module within code - Stack Overflow 

python:

import arcpy
import subprocess
import sys

def install(package):
subprocess.call([sys.executable, "-m", "pip", "install", package])

install('comtypes')


import comtypes,os

def CreateMXD(path):
GetModule('esriCarto.olb')
import comtypes.gen.esriCarto as esriCarto
pMapDocument = CreateObject(esriCarto.MapDocument, esriCarto.IMapDocument)
pMapDocument.New(path)
pMapDocument.Save() #probably not required...

def GetLibPath():
""" Get the ArcObjects library path

It would be nice to just load the module directly instead of needing the path,
they are registered after all... But I just don't know enough about COM to do this

"""
compath=os.path.join(arcpy.GetInstallInfo()['InstallDir'],'com')
return compath

def GetModule(sModuleName):
""" Generate (if not already done) wrappers for COM modules
"""
from comtypes.client import GetModule
sLibPath = GetLibPath()
GetModule(os.path.join(sLibPath,sModuleName))

def CreateObject(COMClass, COMInterface):
""" Creates a new comtypes POINTER object where
COMClass is the class to be instantiated,
COMInterface is the interface to be assigned
"""
ptr = comtypes.client.CreateObject(COMClass, interface=COMInterface)
return ptr

if __name__=='__main__':
#testing...
arcpy.SetProduct('arcview')
filepath='c:/admin/testing123.mxd'
if os.path.exists(filepath):os.unlink(filepath)
CreateMXD(filepath)

JeffBarrette
Status changed to: Closed

We can NOT implement this idea with arcpy/arcpy.mp.  Creating a new project requires a "restart" and because Python gets loaded after the project is being opened, its a bit of a catch-22.  For a similar reason, there are some project options that can't be changed using arcpy.mp because the project needs to be restarted after a change is made.

Jeff - arcpy.mp and Layout teams.