Arcpy: get user's input for location and file name

4052
8
08-31-2018 09:27 AM
helenchu
Occasional Contributor II

Hi

I have arcpy scripts to run in arcmap. Last step is "Table to Excel Conversion"

Instead of having the excel file name and location hard-coded I'd like to get the user's input for those. 

Your help is greatly appreciated!

# sftMap.py
# Created on: 2018-08-31 10:42:32.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True


# Local variables:
SiteNum_AcreCal = "C:\\...\\scratch\\parcel_Select_SpatialJoin_TableToExcel.xls"
parcel_Select_SpatialJoin_Ta_xls = "C:\\...\\ArcGIS\\parcel_Select_SpatialJoin_Ta.xls"

# Process: Table To Excel
arcpy.TableToExcel_conversion(ParcelSiteNum, SiteNum_AcreCal, "NAME", "CODE")

0 Kudos
8 Replies
JoshuaBixby
MVP Esteemed Contributor

If the scripts are running within ArcMap, then The pythonaddins module—Help | ArcGIS Desktop  offers a straightforward way to address your issue.

UPDATE:  The note on the documentation states the module can only be used in Add-ins, but I have used it some with scripts I execute in ArcMap that aren't part of an Addin.  I guess I would have to know more specifics about how the code is run before offering more suggestions.

RandyBurton
MVP Regular Contributor

Interesting approach.

0 Kudos
JonathanQuinn
Esri Frequent Contributor

You can also use arcpy.GetParameter and set the parameter type to a File and the direction as Input. This will allow your user to browse to a file on disk. In a service, you can enable the Uploads capability so the browser can browser to the file to upload as well.

import arcpy

inFile  = arcpy.GetParameter(0)
arcpy.AddMessage(inFile)‍‍‍‍‍‍‍

Input parameter data type set to File

Folder icon opens browser

Path retrieved through variable storing the input parameter:

helenchu
Occasional Contributor II

Thank you so much.  From your example, I created 2 parameters, 1  folder datatype for path and 1 string datatype for filename.  It works!  

0 Kudos
helenchu
Occasional Contributor II

Thanks Joshua.  I was hoping for a more simple approach to my issue.  Most of the time I just use model builder and script out to python.  The code I put here is just the last step of my workflow not the whole thing so I do not think I'm able to write all that in addin.  I've not learned to code in python yet.  I tried to use the output parameter but it didn't work.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Try the addins module, it might work for your regardless of the note in the documentation.  Worst case, you lose an hour or less trying it out.  Another approach is 24. Graphical User Interfaces with Tk — Python 2.7.15 documentation 

RandyBurton
MVP Regular Contributor

Tkinter is also an option.  For destop 10.6:

import Tkinter, tkFileDialog

root = Tkinter.Tk()
root.withdraw()

file_path = tkFileDialog.askopenfilename()

print file_path

Using Joshua's suggestion inside ArcMap (at the bottom of the page he referenced):

import pythonaddins

import os
class MyValidator(object):
    def __str__(self):
        return "Text files(*.txt)"
    def __call__(self, filename):
        if os.path.isfile(filename) and filename.lower().endswith(".txt"):
            return True
        return False

filename = pythonaddins.OpenDialog(r"C:\Path", filter=MyValidator())
helenchu
Occasional Contributor II

Thank you for all your help.  I chose the easiest route for me which is using the built in parameters but all suggestions are very helpful.  I might try them later.