Select to view content in your preferred language

arcpy.conversion.TableToExcel with list

722
3
06-16-2023 03:17 AM
Labels (2)
IlkaIllers1
Occasional Contributor III

Hello!

I have a dataset with a lot of model outputs that ran through an iterator. So it looks a little like this:

A1_abc

A1_xyz

A2_abc

A2_xyz

A3_abc

A3_xyz

And so forth, just a bit more complex.

Now I want to export the tables with the same "suffix" to one Excel table and I wrote the following into the Python window:

arcpy.env.workspace = "(Workspace)"
list = []
name = "abc"
for fc in arcpy.ListFeatureClasses(f"*{name}"):
    list.append(fc)
list.sort()
arcpy.conversion.TableToExcel(list,f"(Output location)\\Location_{name}")

I substituted the workspace path the output location path with (Workspace) and (Output location).

This works fine, though it returns an .xls file. Now if I try to add an .xlsx at the end to the code so it looks like this:

aarcpy.env.workspace = "(Workspace)"
list = []
name = "SensEin_o_Ue_VK_1500"
for fc in arcpy.ListFeatureClasses(f"*{name}"):
    list.append(fc)
list.sort()
arcpy.conversion.TableToExcel(list,f"(Output location)\\Location_{name}.xlsx")

It returns an error message: 

Traceback (most recent call last):
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\TableToExcel.py", line 367, in <module>
table_to_excel(arcpy.GetParameterAsText(0),
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\TableToExcel.py", line 247, in table_to_excel
rowUpdated = list(row)
TypeError: 'list' object is not callable
Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 559, in TableToExcel
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 556, in TableToExcel
retval = convertArcObjectToPythonObject(gp.TableToExcel_conversion(*gp_fixargs((Input_Table, Output_Excel_File, Use_field_alias_as_column_header, Use_domain_and_subtype_description), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Traceback (most recent call last):
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\TableToExcel.py", line 367, in <module>
table_to_excel(arcpy.GetParameterAsText(0),
File "c:\program files\arcgis\pro\Resources\ArcToolbox\scripts\TableToExcel.py", line 247, in table_to_excel
rowUpdated = list(row)
TypeError: 'list' object is not callable

Failed to execute (TableToExcel).

Anyone any ideas why this does not like the .xlsx?

 

 

0 Kudos
3 Replies
BuffaloCoWI
Occasional Contributor

You'll have to call the TableToExcel function in a loop for each value in the list, not just pass a list to the function. It only expects a single value each for the input and output tables.

0 Kudos
IlkaIllers1
Occasional Contributor III

But wouldn't that create separate Excel tables? And why does it work for .xls then?

0 Kudos
BuffaloCoWI
Occasional Contributor

I looked more closely at the TableToExcel documentation, and I do see that the table of parameters shows it should accept a list. (It's pretty subtle, the summary says "a table," i.e. singular, but below it shows the parameter accepted as either "Input_Table" or "[Input_Table,...]".)

If the only difference between your code is .xls vs .xlsx, it should work. I tried it out on a few layers I currently have open, and it worked either way from a Python window in ArcGIS Pro 3.1.2. Perhaps the name you are searching for is not returning the list you're expecting? Maybe print out the list before calling TableToExcel.

0 Kudos