I am trying to join an Excel table to a shapefile in ArcPy. My first step is to convert the Excel sheet to a table, but I am receiving an error:
#Convert xlsx to table:
import os
import xlrd
import arcpy
in_excel= r'C:\\Temp\\Planning\\Planning_Out\\JoinTest.xlsx'
out_gdb= r'C:\\Temp\\Planning\\Planning_Out\\JoinTest.gdb'
def importallsheets(in_excel, out_gdb):
workbook = xlrd.open_workbook(in_excel)
sheets = [sheet.name for sheet in workbook.sheets()]
out_table= os.path.join(out_gdb,arcpy.ValidateTableName("{0}_{1}".format(os.path.basename(in_excel), sheet),out_gdb))
print('Converting {} to {}'.format(sheet, out_table))
# Perform the conversion
arcpy.ExcelToTable_conversion(in_excel, out_table, sheet)
Error:
Traceback (most recent call last):
File "C:\Temp\Planning\LAMP.py", line 52, in <module>
out_table= os.path.join(out_gdb,arcpy.ValidateTableName("{0}_{1}".format(os.path.basename(in_excel), sheet),out_gdb))
NameError: name 'sheet' is not defined
My code was taken from: http://pro.arcgis.com/en/pro-app/arcpy/get-started/working-with-geodatabases.htm
My thought process is that once the Excel file is converted to a table, I can join it to my shapefile using the following code:
# Make the shapefile a layer
arcpy.MakeFeatureLayer_management("merged_lines.shp", "tempLayer")
# Set the local parameters
inFeatures = "tempLayer"
joinField = "Name"
joinTable = r'C:\\Temp\\Planning\\Planning_Out\\JoinTest.gdb '
fieldList = ["Name", "Status"]
# Join feature classes by joinField
arcpy.JoinField_management (inFeatures, joinField, joinTable, joinField, fieldList)
I am using version 10.3.1