2

I am trying to write a Python script that uses a xls file with different spreadsheets as input to build features and domains in a geodatabase.

This is the data in Sheet1:

enter image description here

and this the data in Sheet2

enter image description here

and I would like to use this as input for making features in a geodatabase. Below my script is displayed, but it returns the following error:

string indices must be integers

#---Import modules---# 
    import pandas as pd
    import arcpy
    import xlrd

#---Set paths---#  
    SheetFeatureclass = pd.read_excel(r"C:\xlmtofeature\sheets.xls", sheet_name='Sheet1')
    SheetDomain = pd.read_excel(r"C:\xlmtofeature\sheets.xls", sheet_name='Sheet2')
    projection = r'C:\xlmtofeature\RD New.prj'
    out_path = r'C:\xlmtofeature\test.gdb'

#---Colums in Sheet1---#  
    FEATURENAME = SheetFeatureclass['FEATURENAME']
    GEO_TYPE = SheetFeatureclass['GEO_TYPE']

#---Colums in Sheet2---# 
    DOMEIN = SheetDomain ['DOMEIN']
    FIELDTYPE = SheetDomain['FIELDTYPE']
    DOMEINTYPE = SheetDomain ['DOMEINTYPE']

#---Create features ---#
    for row in SheetFeatureclass:
        try:
            arcpy.CreateFeatureclass_management(out_path, row[FEATURENAME], row[GEO_TYPE], "", "", "",projectie,"","","", "")
            print str(row[FEATURENAME]+ " Toegevoegd" )
        except Exception as err:
            print(err.args[0])
    print "Done with adding features"

#--- create Doamins ---#
    for row in SheetDomain:
        try:
            arcpy.CreateDomain_management(out_path, row[DOMEIN],"",row[FIELDTYPE],row[DOMEINTYPE],"","",)
            print str("Add "+ row[DOMEIN])
        except Exception as err:
            print(err.args[0])
    print "Done with adding Domains"
  • Try using sheetname instead of sheet_name. – TomazicM May 31 at 5:36
0

This sounds like a job for the python xlrd and xlwt modules. You can find their documentation here xlrd and here xlwt.

Opening a single workbook/sheet would be done as follows:

workbook = xlrd.open_workbook('myFile.xls',encoding='utf-8')
worksheet = workbook.sheet_by_name('name_of_sheet')

Getting data is then done as explained in Iterating rows and columns using xlrd in Python. It involves iterating over the rows using the worksheet.ncols and worksheet.nrows attributes.

0

I find that the easiest way to work with Excel spreadsheets and ArcPy is to use the Excel To Table tool which:

Converts Microsoft Excel files into a table.

Once you have a table you have the flexibility to work with it using ArcPy cursors.

The syntax of the Excel To Table tool:

ExcelToTable_conversion (Input_Excel_File, Output_Table, {Sheet})

includes an optional third parameter to allow you to specify a particular worksheet within your spreadsheet.

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.