import osimport arcpy import uuid import openpyxl class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "Toolbox" self.alias = "" # List of tool classes associated with this toolbox self.tools = [Tool] class Tool(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "Tool" self.description = "" self.canRunInBackground = False def getParameterInfo(self): """Define parameter definitions""" param0 = arcpy.Parameter( displayName="FileName", name="filename", datatype="String", parameterType="Required", direction="Input") param0.value = 'OpenPyXl' params = [param0] return params def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): """The source code of the tool.""" wb = openpyxl.Workbook(optimized_write=True) ws = wb.create_sheet() wb_file = r'C:\arcgisserver\directories\arcgisoutput\_ags_' + str(uuid.uuid4()) + '_' + parameters[0].value + '.xlsx' ## arcpy.AddMessage(wb_file) wb.save(wb_file)
Solved! Go to Solution.
import arcpy import os, sys import datetime, time import openpyxl class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "Toolbox" self.alias = "" # List of tool classes associated with this toolbox self.tools = [Tool] class Tool(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "Tool" self.description = "" self.canRunInBackground = False def getParameterInfo(self): """Define parameter definitions""" selectOwners = arcpy.Parameter( displayName = 'This is test 1', name = 'owner_selected', datatype = 'Boolean', parameterType = 'Optional', direction = 'Input') selectResidents = arcpy.Parameter( displayName = 'This is test 2', name = 'residents_selected', datatype = 'Boolean', parameterType = 'Optional', direction = 'Input') params = [selectOwners, selectResidents] return params def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): """The source code of the tool.""" wb = openpyxl.Workbook() filename = os.path.join(arcpy.env.scratchFolder, "myExcel.xlsx") arcpy.AddMessage('Filename : {}'.format(filename)) wb.save(filename)
C:\Python27\ArcGISx6410.1\python.exe
import arcpy
arcpy.ImportToolbox(r"<path to .pyt">
arcpy.<toolname>(params)
import os
import arcpy
import sys
import xlwt
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
params = None
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
arcpy.AddMessage('Version : %s' % sys.version)
arcpy.AddMessage('Path to module : %s' % os.path.dirname(xlwt.__file__))
arcpy.AddMessage('End')
print 'Version : %s' % sys.version
print 'Path to module : %s' % os.path.dirname(xlwt.__file__)
print 'End'
But when I then share the result as a Service, it never finishes, it keeps on running at 175kb.
import arcpyimport os, sys
import datetime, time
import uuid
from arcpy import env
import openpyxl
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
selectOwners = arcpy.Parameter(
displayName = 'This is test 1',
name = 'owner_selected',
datatype = 'Boolean',
parameterType = 'Optional',
direction = 'Input')
selectResidents = arcpy.Parameter(
displayName = 'This is test 2',
name = 'residents_selected',
datatype = 'Boolean',
parameterType = 'Optional',
direction = 'Input')
params = [selectOwners, selectResidents]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
scriptPath = sys.path[0]
outfolder = os.path.join(scriptPath, 'Output')
arcpy.env.scratchWorkspace = scriptPath
wb = openpyxl.Workbook()
filename = '_ags_' + str(uuid.uuid4()) + '.xlsx'
arcpy.AddMessage('Filename : %s' % os.path.join(outfolder, filename))
wb.save(os.path.join(outfolder, filename))
import arcpy import os, sys import datetime, time import openpyxl class Toolbox(object): def __init__(self): """Define the toolbox (the name of the toolbox is the name of the .pyt file).""" self.label = "Toolbox" self.alias = "" # List of tool classes associated with this toolbox self.tools = [Tool] class Tool(object): def __init__(self): """Define the tool (tool name is the name of the class).""" self.label = "Tool" self.description = "" self.canRunInBackground = False def getParameterInfo(self): """Define parameter definitions""" selectOwners = arcpy.Parameter( displayName = 'This is test 1', name = 'owner_selected', datatype = 'Boolean', parameterType = 'Optional', direction = 'Input') selectResidents = arcpy.Parameter( displayName = 'This is test 2', name = 'residents_selected', datatype = 'Boolean', parameterType = 'Optional', direction = 'Input') params = [selectOwners, selectResidents] return params def isLicensed(self): """Set whether tool is licensed to execute.""" return True def updateParameters(self, parameters): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parameter has been changed.""" return def updateMessages(self, parameters): """Modify the messages created by internal validation for each tool parameter. This method is called after internal validation.""" return def execute(self, parameters, messages): """The source code of the tool.""" wb = openpyxl.Workbook() filename = os.path.join(arcpy.env.scratchFolder, "myExcel.xlsx") arcpy.AddMessage('Filename : {}'.format(filename)) wb.save(filename)
import os
import arcpy
import openpyxl
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter(
displayName = 'Filename',
name = 'filename',
datatype = 'String',
parameterType = 'Required',
direction = 'Input')
param0.value = 'TestFile'
params = [param0]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
fn = parameters[0].value
wb = openpyxl.Workbook()
filename = 'C:/arcgisserver/directories/arcgisoutput/' + fn + '.xlsx'
arcpy.AddMessage('Filename : %s' % filename)
wb.save(filename)
return