Here's what I am trying to do: I want to move feature classes from one .gdb (OLD) to another (NEW), but only the feature classes that are present in an excel spreadsheet.

  1. read from excel spreasheet (old name column)
  2. compare to old .gdb feature classes to find match
  3. move matched feature class to new .gdb
  4. rename feature class in new .gdb based on new name in excel spreadsheet (new name column)
  5. continue doing the above until the end of the spread sheet
  • I would convert the .xls file to a .txt file and then when reading in each line, split by comma (make sure none of your cells have commas so it doesn't throw off your split). Add the value from "old name column" by line.split(",")[column number in excel spreadsheet goes here] (indexes start at zero) to a list and loop through your list. Then leverage arcgisscripting at 9.3. – Justin Jul 3 '12 at 21:10
  • 1
    Instead of reading a text file and splitting lines, I would recommend exporting to CSV and using the Python csv module to read the file. This automatically splits the lines into lists or dictionaries depending on which reader you use. – blah238 Jul 3 '12 at 22:05
  • If you install pyodbc you don't even need to go as far as saving as a CSV - you can just connect directly using something like conn = pyodbc.connect('DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;Readonly=1' % excel_path, autocommit=True) – om_henners Jul 3 '12 at 22:57
  • 1
    I may be mistaken, but can't you connect directly to a XLS in 9.3 and then just run a SearchCursor directly on it? I'm running 10.0 or I'd just test it. – Chad Cooper Jul 3 '12 at 23:51
  • @Chad, I think you are correct. – blah238 Jul 6 '12 at 1:42

Assuming you have the pyodbc library installed (there's an installer for Python 2.5 in their Downloads section) it's a simple matter of looping through each row of the Excel sheet. Something like:

#import libraries
import arcgisscripting
import pyodbc
import os.path

#set paths to documents
old_gdb = "path_to_old.gdb"
new_gdb = "path_to_new.gdb"
excel_path = "path_to.xls"

#initialise arcgis
gp = arcgisscripting.create(9.3)

#create a connection to the excel sheet
conn = pyodbc.connect('DRIVER={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=%s;Readonly=1' % excel_path, autocommit=True)

#Create the cursor
cursor = conn.cursor()

#Loop over all the rows returned from a cursor execute
for row in cursor.execute('select * from "Sheet1$"'):
    #we can reference columns by their name in the row object
    if gp.Exists(os.path.join(old_gdb, row.old_name_column)):
        gp.copy_management(os.path.join(old_gdb, row.old_name_column), os.path.join(new_gdb, row.new_name_column))
        print "Some warning"

If you don't want to use pyodbc, it should be easy enough to substitute in a csv reader or otherwise instead of the connection to the .xls. Good luck!

  • thanks for your help. I tried running your code and it came up with this error Traceback (most recent call last): File "H:\SETI\Python_Script\excelTest.py", line 18, in <module> for row in cursor.execute('select * from "Sheet1$"'): NameError: name 'cursor' is not defined what does it mean? Please keep in mind I have little experience with python scripting for ArcGIS – Viktor Jul 5 '12 at 15:51
  • @Viktor Whoops! My bad! I forgot to put in the creation of the cursor object. It's in the code now. Sorry about that! As far as the error goes, a NameError typically means an object hasn't been created before you try and use it. – om_henners Jul 5 '12 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.