I am trying to convert a Python script I've made into a tool to make it easier for people not familiar with Python to use.

The job of the script is to take a template mxd with the background layers and layout view already made and upload a CSV to it and run a series of analysis and export a map.

I was wondering if it was possible to create a tool that could work inside of a closed mxd, or if that could only be done through Python.

Python script code snippet:

mxd = arcpy.mapping.MapDocument(r'Template mxd')
df = arcpy.mapping.ListDataFrames(mxd)[0]

# adds all background layers
addLayer = arcpy.mapping.Layer(r"background layer 1")
arcpy.mapping.AddLayer(df, addLayer,"BOTTOM")

addLayer = arcpy.mapping.Layer(r"background layer 2")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

... more layers added ...

# Change coordinate system

prjPath = "c:\program files (x86)\arcgis\desktop10.2\Coordinate Systems\Projected Coordinate Systems\Continental\North America\Canada Lambert Conformal Conic.prj"
spatial_ref = arcpy.SpatialReference(prjPath)
df.spatialReference = spatial_ref

#upload excel file and display x/y coordinates

arcpy.env.workspace = r"Template mxd"
arcpy.TableToTable_conversion(outLayer, table2table, "temp")

x_coords = "Longitude"
y_coords = "Latitude"
out_Layer = "outLayer"
prjPath2 = "c:\program files (x86)\arcgis\desktop10.2\Coordinate Systems\Geographic Coordinate Systems\North America\NAD 1983.prj"
arcpy.MakeXYEventLayer_management (in_Table, x_coords, y_coords, out_Layer, prjPath2)

arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)
addLayer = arcpy.mapping.Layer(saved_Layer)
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

This isn't the full code but it's just a sample and hopefully it shows what I mean when I say a closed (not currently open) mxd.

  • 2
    Are you asking to create a script that you would run outside arcmap in something like IDLE to process an mxd that is not currently open in ArcMap, if so then yes. – Hornbydd Aug 9 '18 at 17:22
  • Sorry Hornbydd that's not what I'm asking. I currently have a script that does exactly that. I'm wondering if it's possible to make a tool that does the same thing. – Daniel Mutton Aug 9 '18 at 17:32
  • Can you please include a code snippet that illustrates your Python script and current vs desired coding pattern? – PolyGeo Aug 9 '18 at 22:52
  • 1
    I'm having a problem with the "work inside a closed mxd" problem statement. Nothing can work inside a closed document and you can't use it without opening it. If you have a standalone script, it doesn't need to be inside ArcMap; if you want a Python Toolbox it does (unless you rig the .pyt for standalone execution) – Vince Aug 10 '18 at 11:16
  • It’s not entirely clear what you are asking. However you can make your Python script run from a double-click in Windows Explorer by wrapping it in a batch script. If the script requires user input, you can do it either with a text prompt, or you can create a GUI using Tk or Tix or similar. Setting up an interactive GUI can be a bit tedious though. Your script is actually opening your MXD without opening ArcMap, so technically it’s not really working outside of a closed MXD. – Son of a Beach Aug 10 '18 at 23:35

You can just add the script as a script tool to any toolbox, or integrate it into a Python Toolbox.

How to add a script tool to a toolbox: ESRI Help

Python Toolbox Intro: ESRI Help

You'll just need to make sure your script is appropriately set up to accept the required parameters from the toolbox dialog, and of course set up the dialog to have your parameters passed to the script.

Without knowing more details about your existing code, it's difficult to give a better answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.