2

I'm using python scripts on ArcGIS 10.2 to read excel files with information and put this results on attribute tables of the ArcGIS.

I know how to create a table, add rows, add fields and send information to tables.

from arcpy import env
env.workspace = "F:Tr/Model/Results"
output="Tr/Model/Results"
arcpy.CreateTable_management(output, "ger_rea.dbf")

#Create fields
arcpy.env.workspace = "F:Tr\Model\Results"
arcpy.AddField_management("ger_rea.dbf", "Barr", "Text", 50, "", "", "refcode", "NULLABLE", "REQUIRED")
arcpy.AddField_management("ger_rea.dbf", "Ge_1", "Float",  "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management("ger_rea.dbf", "Ge_2", "Float",  "", "", 10, "", "NON_NULLABLE", "NON_REQUIRED", "")

table_ge_rea="F:Tr\Model\Results\ger_rea.dbf" 

#Add rows
rows_pa = arcpy.InsertCursor("F:Tr\Model\Results\ger_rea.dbf")
for x in range(1, (L_Nod_name_LA+1)):
    row = rows_pa.newRow()
    rows_pa.insertRow(row)

#Put information on created table
gr=0
with arcpy.da.UpdateCursor(table_ge_rea,["Barr","OID"]) as cursor:
    for row in cursor:
        row[0]=Nod_name_GA[gr]
        row[1]=Act_L_value[ga]
        cursor.updateRow(row)
        gr=gr+1

However I need to create an automatically tool and The number os rows and fields of the excel tables can change and consequently tables that I'm creating with arcpy need to change too. For example: where I had a table with fields "Barr", "Ge_1", "Ge_2" I can have "Barr", "Ge_1", "Ge_2", "Ge_3", "Ge_4", etc...

How can I create a table (with arcpy) automatically, knowing the number of fields and rows (information on excel that I know how to read), using a for or while loop?

I tryed also use "arcpy excel to table" to export directly a sheet of an excel file to ArcGIS, using this code:

import arcpy
arcpy.env.workspace = "F:\Otim\inter"
arcpy.ExcelToTable_conversion("02_Reactiv.xlsx", "outger.gdb", "PoGenRe")

where 02_Reactiv.xlsx is the excel file, outger.gdb is the name of the output table and PoGenRe is the name of the sheet that I want to pass to ArcGIS. This sheet contain: on the firts line only two columns filled, and Below, the information that I want to pass: 13 lines, 3 columns, 4 columns in blank and 2 columns with sum of some values, etc..

It gives me an error:

Runtime error Traceback (most recent call last): File "", line 1147, in File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 44, in ExcelToTable raise e ExecuteError: Traceback (most recent call last): File "c:\program files (x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line 254, in arcpy.GetParameterAsText(2)) File "c:\program files (x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line 205, in excel_to_table out_fields = gen_out_fields(workbook, sheet, out_path, is_gdb) File "c:\program files (x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line 177, in gen_out_fields out_fields.append(clsField(f, out_path, is_gdb, out_fields)) File "c:\program files (x86)\arcgis\desktop10.2\ArcToolbox\Scripts\ExcelToTable.py", line 32, in init if not name[0].isalpha(): IndexError: string index out of range Failed to execute (ExcelToTable).

  • Is there a requirement that you have which is not met by the Excel To Table tool? – PolyGeo Aug 22 '16 at 10:03
  • I tryed this: import arcpy arcpy.env.workspace = "F:\Otimizacao\interface" arcpy.ExcelToTable_conversion("02_Rea.xlsx", "outger.gdb", "PGenRe") But gives me an error.. and didn't run – S.Rai Aug 22 '16 at 10:42
  • 1
    Please edit the question to contain this information and the exact error, and the exact contents of the first few rows of the table, so that your question is answerable. – Vince Aug 22 '16 at 11:19
  • I edited @Vince – S.Rai Aug 22 '16 at 11:39
  • @PolyGeo, my problem is, when I use Excel To Table tool I can't represent values of this table on map, because I don't have coordinates or other reference of values that I want put on map.. Do you understand? – S.Rai Aug 22 '16 at 16:04
5

Your arcpy.env.workspace = "F:\Otim\inter" slashes are not correct. Use one of these instead:

arcpy.env.workspace = r"F:\Otim\inter"

or

arcpy.env.workspace = "F:\\Otim\\inter"

or

arcpy.env.workspace = "F:/Otim/inter"

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.