Select to view content in your preferred language

Table to Excel within Python script

13837
23
Jump to solution
06-16-2015 02:16 PM
CoyPotts1
Deactivated User

I'm trying to export my feature class to a table, and then also create a copy as an Excel document.  The following code keeps kicking back with an error saying that I am using the wrong file type.

Error message:

"Failed to execute. Parameters are not valid.

ERROR 000814: Invalid file type

Failed to execute (TableToExcel)."

Code:

##########
# Create a file geodatabase for attribute tables to be saved in
##########


# Set local variables - uses variables derived earlier in script
attFolder = mapFolder + "/" + "6)Deliverables/4)Feature_Class_Attribute_Tables/" + version
outATTgdb = 'attribute_tables.gdb'

# Execute CreateFileGDB
arcpy.CreateFileGDB_management(attFolder, outATTgdb, 'CURRENT')


##########
# General Features export
##########

# Set local variables
inTable = generalFeatures
exportLoc = attFolder + "/" + outATTgdb
outTable = "general_attributes"
outXLS = "general_attributes.xls"

# Execute TableToTable
arcpy.TableToTable_conversion(inTable, exportLoc, outTable)

arcpy.env.workspace = 'attFolder'

# Execute TableToExcel
arcpy.TableToExcel_conversion(inTable, outXLS)









I've tried modifying this a bit, with no positive results.  For the outXLS variable I tried to make it equal the entire save location, and then removed the arcpy.env.workspace variable, but everything that I have tried results in the same "invalid file type" error.

Any recommendations?

Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
CoyPotts1
Deactivated User

I was looking at this the wrong way.  I was assuming that the error was referring to the input table that was incorrect, but it was actually referring to the output file as being incorrect.  Instead of simply giving the output a name, I gave it a save location with a name, and it worked just fine.  See the code below:

# Set local variables
inTable = outTable
outXLS = kmfFolder + "/general_KMF.xls" #this is where I had to give the file path and then the file name

# Execute TableToExcel
arcpy.TableToExcel_conversion(inTable, outXLS)
ersion(inTable, outXLS)

View solution in original post

0 Kudos
23 Replies
BlakeTerhune
MVP Regular Contributor

The TableToExcel tool requires an input of a table view, not just the path to a table. Try making the table view first, then use that as input for the table to Excel process.

0 Kudos
CoyPotts1
Deactivated User

I'll have to play around with the table view.

Essentially I am porting over a very large model builder model.  Following the workflow in order as it is in Model Builder, I just used the same table name thinking it would work.  I'll try this with the table view option when I get a little more time to tinker with it and I'll respond with my results.

Thanks!

0 Kudos
BlakeTerhune
MVP Regular Contributor

I just tried it without a Table View and it worked. Here's my code.

import arcpy
import os

myDir = r"C:\Temp"
myGDB = "TEMP.gdb"
myTable = os.path.join(myDir, myGDB, "SomeTableName")
myExcel = os.path.join(myDir, "myExcel.xls")

arcpy.TableToExcel_conversion(myTable, myExcel)
IngridMans
Frequent Contributor

Is this why my standalone script doesn't produce an output excel table, but the same code works when run within ArcGIS Pro? This behavior seems to only have started after upgrading to ArcPro 2.7. I have a script that runs a series of steps to update a feature class, and at the end I like to save a copy of the table as an Excel file. The code points to a path for the table, and it always says it runs successfully (or at least it doesn't throw any errors), but when I go looking for the output table in Windows file explorer, nothing is there. 

0 Kudos
IanMurray
Honored Contributor

What sort of table is your input for Table to Excel?

0 Kudos
CoyPotts1
Deactivated User

I am taking a feature class and running the Table to Table tool to save it as a data table.  Then I am taking that data table and exporting it to Excel.

0 Kudos
DarrenWiens2
MVP Honored Contributor

This points to a string 'attFolder':

arcpy.env.workspace = 'attFolder'  

but you want the variable attfolder:

arcpy.env.workspace = attFolder

BlakeTerhune
MVP Regular Contributor

Good spot, Darren. On that note, the Python OS module can create file paths more reliably than concatenating strings. Try os.path.join()

CoyPotts1
Deactivated User

Good catch, Darren.

Blake, how would I construct this when I am using variables and added strings?  I didn't fully understand the examples in the provided link.  Also, why would concatenating strings be unreliable?  Just trying to understand.

Thanks to both of you!

0 Kudos