yes, sure. I will upload two different scripts, one is for ArcMap, and another one is for ArcGIS Pro.
Below is the ArcMap version.
import arcpy
from arcpy import env
import sys
import os
import openpyxl
from subprocess import Popen
env.overwriteOutput = True
input = arcpy.GetParameterAsText(0)
fc1234_boolean = arcpy.GetParameterAsText(1)
xlsx_boolean = arcpy.GetParameterAsText(2)
inputlist = input.split(";")
inputfile_first = inputlist[0]
inputlyr = arcpy.mapping.Layer(inputfile_first)
inputpath = inputlyr.dataSource
inputpath0 = os.path.split(inputpath)[0]
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
home_f = os.path.dirname(mxd.filePath)
testcode = os.path.basename(home_f)
year = testcode.split("_")[0]
country = testcode.split("_")[1]
city_id = testcode.split("_")[2]
cityshort = testcode.split("_")[3]
city_index = testcode.split("_")[4]
testcodeshort = year + '_' + cityshort
working_f = home_f + '\\Working_folder\\'
working_space = arcpy.env.workspace
dr_file = home_f + '\\NAV\\Drive_route\\' + testcode + "_driveroute.shp"
streets_nav = testcodeshort + "_Streets_NAV"
streets_nav_fc1234 = testcodeshort + "_Streets_NAV_FC1234"
streets_nav_fc1234_long = testcode + "_Streets_NAV_FC1234"
rdms = testcodeshort + "_RDM_line"
rdms_fc12 = testcodeshort + "_RDM_line_FC12"
rdms_fc34 = testcodeshort + "_RDM_line_FC34"
ows = testcodeshort + "_OW"
ows_fc12 = testcodeshort + "_OW_FC12"
ows_fc34 = testcodeshort + "_OW_FC34"
Geo_Stats = working_space + "\\Geo_Stats"
OW_Stats = working_space + "\\OW_Stats"
RDM_Stats = working_space + "\\RDM_Stats"
linkidList = []
FC1len = 0
FC2len = 0
FC3len = 0
FC4len = 0
FC5len = 0
owFC1len = 0
owFC2len = 0
owFC3len = 0
owFC4len = 0
owFC5len = 0
rdmFC1cnt = 0
rdmFC2cnt = 0
rdmFC3cnt = 0
rdmFC4cnt = 0
rdmFC5cnt = 0
if fc1234_boolean.lower() != 'true':
arcpy.AddMessage("Making selection")
for fc in inputlist:
with arcpy.da.SearchCursor(fc, ["LINK_ID"]) as cursor:
for row in cursor:
linkidList.append(row[0])
where = '"LINK_ID" = {0}'.format(int(linkidList[0]))
del linkidList[0]
for linkid in linkidList:
where += ' OR "LINK_ID" = {0}'.format(int(linkid))
arcpy.SelectLayerByAttribute_management(streets_nav, "NEW_SELECTION", where)
arcpy.SelectLayerByAttribute_management(ows, "NEW_SELECTION", where)
arcpy.SelectLayerByAttribute_management(rdms, "NEW_SELECTION", where)
else:
arcpy.AddMessage("Making selection FC1234")
layerList = []
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
layerList.append(lyr.name)
for fc in inputlist:
with arcpy.da.SearchCursor(fc, ["LINK_ID"]) as cursor:
for row in cursor:
linkidList.append(row[0])
where = '"LINK_ID" = {0}'.format(int(linkidList[0]))
del linkidList[0]
for linkid in linkidList:
where += ' OR "LINK_ID" = {0}'.format(int(linkid))
if streets_nav_fc1234 in layerList:
streets_layer = streets_nav_fc1234
elif streets_nav_fc1234_long in layerList:
streets_layer = streets_nav_fc1234_long
arcpy.SelectLayerByAttribute_management(streets_layer, "NEW_SELECTION", where)
if ows_fc12 in layerList:
arcpy.SelectLayerByAttribute_management(ows_fc12, "NEW_SELECTION", where)
if ows_fc34 in layerList:
arcpy.SelectLayerByAttribute_management(ows_fc34, "NEW_SELECTION", where)
if rdms_fc12 in layerList:
arcpy.SelectLayerByAttribute_management(rdms_fc12, "NEW_SELECTION", where)
if rdms_fc34 in layerList:
arcpy.SelectLayerByAttribute_management(rdms_fc34, "NEW_SELECTION", where)
df.zoomToSelectedFeatures()
df.scale = df.scale * 1.1
if xlsx_boolean.lower() == 'true':
if fc1234_boolean.lower() == 'true':
arcpy.AddError(
"Not possible to write only FC1234 attributes to an .xlsx file. Run tool again without this option")
sys.exit()
arcpy.AddMessage("Calculating counts for .xlsx")
arcpy.Statistics_analysis(streets_nav, Geo_Stats, "LEN SUM", "FUNC_CLASS")
rows = arcpy.SearchCursor(Geo_Stats, "FUNC_CLASS = '1'", "", "", "")
for row in rows:
FC1len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(Geo_Stats, "FUNC_CLASS = '2'", "", "", "")
for row in rows:
FC2len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(Geo_Stats, "FUNC_CLASS = '3'", "", "", "")
for row in rows:
FC3len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(Geo_Stats, "FUNC_CLASS = '4'", "", "", "")
for row in rows:
FC4len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(Geo_Stats, "FUNC_CLASS = '5'", "", "", "")
for row in rows:
FC5len = row.getValue("SUM_LEN")
geoLen12 = FC1len + FC2len
geoLen34 = FC3len + FC4len
geoLen5 = FC5len
arcpy.Statistics_analysis(ows, OW_Stats, "LEN SUM", "FUNC_CLASS")
rows = arcpy.SearchCursor(OW_Stats, "FUNC_CLASS = '1'", "", "", "")
for row in rows:
owFC1len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(OW_Stats, "FUNC_CLASS = '2'", "", "", "")
for row in rows:
owFC2len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(OW_Stats, "FUNC_CLASS = '3'", "", "", "")
for row in rows:
owFC3len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(OW_Stats, "FUNC_CLASS = '4'", "", "", "")
for row in rows:
owFC4len = row.getValue("SUM_LEN")
rows = arcpy.SearchCursor(OW_Stats, "FUNC_CLASS = '5'", "", "", "")
for row in rows:
owFC5len = row.getValue("SUM_LEN")
owLen12 = owFC1len + owFC2len
owLen34 = owFC3len + owFC4len
owLen5 = owFC5len
arcpy.Statistics_analysis(rdms, RDM_Stats, "LEN SUM", "FUNC_CLASS")
rows = arcpy.SearchCursor(RDM_Stats, "FUNC_CLASS = '1'", "", "", "")
for row in rows:
rdmFC1cnt = row.getValue("FREQUENCY")
rows = arcpy.SearchCursor(RDM_Stats, "FUNC_CLASS = '2'", "", "", "")
for row in rows:
rdmFC2cnt = row.getValue("FREQUENCY")
rows = arcpy.SearchCursor(RDM_Stats, "FUNC_CLASS = '3'", "", "", "")
for row in rows:
rdmFC3cnt = row.getValue("FREQUENCY")
rows = arcpy.SearchCursor(RDM_Stats, "FUNC_CLASS = '4'", "", "", "")
for row in rows:
rdmFC4cnt = row.getValue("FREQUENCY")
rows = arcpy.SearchCursor(RDM_Stats, "FUNC_CLASS = '5'", "", "", "")
for row in rows:
rdmFC5cnt = row.getValue("FREQUENCY")
rdmCnt12 = rdmFC1cnt + rdmFC2cnt
rdmCnt34 = rdmFC3cnt + rdmFC4cnt
rdmCnt5 = rdmFC5cnt
count_dr = arcpy.GetCount_management(dr_file)
count_dr_int = int(count_dr.getOutput(0))
if os.path.isfile(
r'C:\Program Files (x86)\ArcGIS\Desktop10.5\Styles\Prep_layer_definition\Calc_templates\calc_dr_selection_temp.xlsx'):
dr_selection_temp = openpyxl.load_workbook(
r'C:\Program Files (x86)\ArcGIS\Desktop10.5\Styles\Prep_layer_definition\Calc_templates\calc_dr_selection_temp.xlsx')
else:
dr_selection_temp = openpyxl.Workbook()
sheet = dr_selection_temp.get_active_sheet()
sheet['D3'] = geoLen12
sheet['F3'] = geoLen34
sheet['H3'] = geoLen5
sheet['D4'] = owLen12
sheet['F4'] = owLen34
sheet['H4'] = owLen5
sheet['D5'] = rdmCnt12
sheet['F5'] = rdmCnt34
sheet['H5'] = rdmCnt5
sheet['D6'] = geoLen12
sheet['F6'] = geoLen34
sheet['H6'] = geoLen5
sheet['B13'] = count_dr_int
try:
arcpy.AddMessage("Selection saved in {0}_calc_dr_selection.xlsx (in Working_folder)".format(testcode))
dr_selection_temp.save(working_f + '\\' + testcode + '_calc_dr_selection.xlsx')
p = Popen(working_f + '\\' + testcode + '_calc_dr_selection.xlsx', shell=True)
except:
arcpy.AddError(testcode + "_calc_dr_selection.xlsx is already open. Close it first before running the tool")
sys.exit()