Python Forum

Full Version: Read directly from excel file using python script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have iterated over a text file to create a dataset with feature classes in a new gdb. I am having trouble trying to figure out how to modify the script to do this reading directly off an excel file. I would convert it but I have to do it directly. My spreadsheet has 3 worksheets but I need it to work if more are added later also.
import arcpy, fileinput, os
arcpy.env.workspace = "C:/path"
folder = "C/path"
database = "GPS_Results.gdb"
fdataset = "Study_Areas"
spatialref = arcpy.SpatialReference("NAD 1983 UTM Zone 12N")
arcpy.CreateFileGDB_management(folder + "/", database)
arcpy.CreateFeatureDataset_management(os.path.join(folder, database), fdataset, spatialref)
for textfile in arcpy.ListFiles("*.txt"):
    newfc = os.path.splitext(textfile)[0]
    outpath = os.path.join(folder, database, fdataset)
    arcpy.CreateFeatureclass_management(outpath, newfc, "POINT")
    cursor = arcpy.da.InsertCursor(os.path.join(outpath, newfc), ["SHAPE@"])
    point = arcpy.Point()
    infile = os.path.join(folder, textfile)
    skipline = 0
    for line in fileinput.input(infile):
        if skipline != 0:
            point.ID, point.X, point.Y = line.split( )
            cursor.insertRow([point])
        else:
            skipline += 1
    del cursor