Export layers of a gdb as excel table

3345
22
Jump to solution
02-16-2021 05:06 PM
JoelRodriguez95
New Contributor II

Hello everybody,

I'm new in this world of arcpy and I would like poder export all of layer of a gbd to an excel format, I tried with several this foro's scripts but I can't get the result, this is my script actual but not funcionate.

import arcpy

from arcpy import env

env.workspace = r" C:\Users\LAP77\Desktop\JR\Veg_Hidro_Final.gdb"

output_folder = of = r" C:\Users\LAP77\Desktop\JR\Veg_Hidro_Final\Excel"

for fc in arcpy.ListFeatureClasses("*"):

        fc_name= of+ "\\"+fc+".xls" 

        print(fc_name)

        arcpy.TableToExcel_conversion(fc,fc_name, "NAME", "CODE")

 

0 Kudos
3 Solutions

Accepted Solutions
by Anonymous User
Not applicable

for the already exists error, you can del it at the end of the iteration or you can add

env.overwriteOutput = True

under the imports so it overwrites the outputs.

View solution in original post

DanPatterson
MVP Esteemed Contributor

spaces in the path.... isn't a path if it is the first character especially

raw encoding won't help for that either


... sort of retired...

View solution in original post

0 Kudos
JoelRodriguez95
New Contributor II

Thanks for your time, and I'm sorry for not verified the spaces.

import os
import arcpy
from arcpy import env

env.workspace = r"C:\data.gdb"
output_folder = r"C:\output"

for fc in arcpy.ListFeatureClasses("*"):
    out_xls = os.path.join(output_folder, r'{}.xls'.format(fc))
    env.overwriteOutput = True
    fc_path = os.path.join(env.workspace, fc)
    fc_table_view = arcpy.MakeTableView_management(fc_path, "tview")
    arcpy.TableToExcel_conversion(fc_table_view, out_xls)

 

View solution in original post

0 Kudos
22 Replies
DavinWalker2
Esri Contributor

What error are you getting when you run the script?

JoelRodriguez95
New Contributor II

Thanks for your time!

This is the error:

>>> import arcpy
... from arcpy import env
... env.workspace = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Veg_Hidro_Final.gdb"
... output_folder = of = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Excel"
... for fc in arcpy.ListFeatureClasses("*"):
...         fc_name= of+ "\\"+fc+".xls"	
...         print(fc_name)
...         arcpy.TableToExcel_conversion(fc,fc_name, "NAME", "CODE")
...         
 C:\Users\LAP77\JR\Veg_Hidro_Final\Excel\Veg_Hidro_Final.xls
Runtime error  Traceback (most recent call last):   File "<string>", line 8, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 81, in TableToExcel     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset Veg_Hidro_Final does not exist or is not supported Failed to execute (TableToExcel).  
0 Kudos
by Anonymous User
Not applicable

TableToExcel takes a Table View as an input.  You need to make a table view from your fc and pass that into the function.  See my post below for the full code.

fc_table_view = arcpy.MakeTableView_management(fc, "tview")
arcpy.TableToExcel_conversion(fc_table_view, out_xls)

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

output_folder

fc_name= of+ "\\"+fc+".xls" 

is "of" short for output_folder ?


... sort of retired...
0 Kudos
DavinWalker2
Esri Contributor

Yes the OP has assigned the variable like this

output_folder = ofr" C:\Users\LAP77\Desktop\JR\Veg_Hidro_Final\Excel"

DanPatterson
MVP Esteemed Contributor

Table To Excel (Conversion)—ArcGIS Pro | Documentation

make sure you specify whether you want an xls or xlsx as well.  There are limitations with xls format

Converts a table to a Microsoft Excel file (.xls or .xlsx).

In any event, throw in a print statement to make sure you get a valid output filename when you are testing.

And on a different note.  you have a space at the beginning of your paths.

 

os.path.isdir(" C:\\Temp")  # --- space preceeding path
False

os.path.isdir("C:\\Temp")   # --- no space
True

unless it is a code formatting issue... for future reference

Code formatting ... the Community Version - GeoNet, The Esri Community

 


... sort of retired...
0 Kudos
JoelRodriguez95
New Contributor II

Thanks for your time, It's not a code formatting issue because when I print the ListFeatureClasses is all okay, but thank you very much for the Code Formatting for this post!

 

0 Kudos
by Anonymous User
Not applicable

I just caught that you wanted excel format so I changed the example code.  TableToExcel takes a table view, which is missing from your code.

updated per Blake's note on describe.

 

import os
import arcpy
from arcpy import env

env.workspace = r" C:\Users\LAP77\Desktop\JR\Veg_Hidro_Final.gdb"
output_folder = r" C:\Users\LAP77\Desktop\JR\Veg_Hidro_Final\Excel"

for fc in arcpy.ListFeatureClasses("*"):
    print(fc)
    out_xls = os.path.join(output_folder, fr'{fc}.xls')
    fc_table_view = arcpy.MakeTableView_management(fc, "tview")
    arcpy.TableToExcel_conversion(fc_table_view, out_xls)

 

BlakeTerhune
MVP Regular Contributor

Just a couple small notes on ListFeatureClasses().  It returns the feature class names as strings in a list so you don't need to Describe() them. You can also exclude the wild_card parameter as the default will return everything.