Export layers of a gdb as excel table

3330
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
22 Replies
JoelRodriguez95
New Contributor II

Thanks for yout time! I tried with this script but I get the following error:

Parsing error SyntaxError: invalid syntax (line 10)



0 Kudos
by Anonymous User
Not applicable

ok could a difference between python 2 and 3. Change it to:

 

out_xls = os.path.join(output_folder, r'{}.xls'.format(fc))

 

 

0 Kudos
JoelRodriguez95
New Contributor II

sorry for the inconvenience :(, but it throws me error:

>>> import os
... import arcpy
... from arcpy import env
... 
... env.workspace = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Veg_Hidro_Final.gdb"
... output_folder = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Excel"
... 
... for fc in arcpy.ListFeatureClasses("*"):
...     print(fc)
...     out_xls = os.path.join(output_folder, r'{}.xls'.format(fc))
...     print(out_xls)
...     fc_table_view = arcpy.MakeTableView_management(fc,"tview")
...     arcpy.TableToExcel_conversion(fc_table_view, out_xls)
...     
 C:\Users\LAP77\JR\Veg_Hidro_Final\Excel\Veg_Hidro_Final.xls
Runtime error  Traceback (most recent call last):   File "<string>", line 12, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 6966, in MakeTableView     raise e ExecuteError: ERROR 000732: Input Table: Dataset Veg_Hidro_Final does not exist or is not supported  

these are the feature classes in the gdb:

JoelRodriguez95_0-1613582247530.png

and this a part of table's Veg_Hidro_Final

JoelRodriguez95_2-1613582443324.png

 

 

0 Kudos
by Anonymous User
Not applicable

Try with the full path to the fc:

...
out_xls = os.path.join(output_folder, r'{}.xls'.format(fc))
print(out_xls)
fc_path = os.path.join(env.workspace, fc)
fc_table_view = arcpy.MakeTableView_management(fc_path, "tview")

 

0 Kudos
JoelRodriguez95
New Contributor II

nothing 😞

>>> import os
... import arcpy
... from arcpy import env
... 
... env.workspace = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Veg_Hidro_Final.gdb"
... output_folder = r" C:\Users\LAP77\JR\Veg_Hidro_Final\Excel"
... 
... for fc in arcpy.ListFeatureClasses("*"):
...     print("1::",fc)
...     out_xls = os.path.join(output_folder, r'{}.xls'.format(fc))
...     print("2::",out_xls)
...     print("3::",fc, out_xls)
...     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)
...     
('1::', u'Veg_Hidro_Final')
('2::', ' C:\\Users\\LAP77\\JR\\Veg_Hidro_Final\\Excel\\Veg_Hidro_Final.xls')
('3::', u'Veg_Hidro_Final', ' C:\\Users\\LAP77\\JR\\Veg_Hidro_Final\\Excel\\Veg_Hidro_Final.xls')
Runtime error  Traceback (most recent call last):   File "<string>", line 15, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\conversion.py", line 81, in TableToExcel     raise e ExecuteError:  Traceback (most recent call last):   File "c:\program files (x86)\arcgis\desktop10.3\ArcToolbox\Scripts\TableToExcel.py", line 222, in <module>     arcpy.GetParameter(3))   File "c:\program files (x86)\arcgis\desktop10.3\ArcToolbox\Scripts\TableToExcel.py", line 216, in table_to_excel     workbook.save(output)   File "C:\Python27\ArcGIS10.3\lib\site-packages\xlwt\Workbook.py", line 662, in save     doc.save(filename, self.get_biff_data())   File "C:\Python27\ArcGIS10.3\lib\site-packages\xlwt\CompoundDoc.py", line 261, in save     f = open(file_name_or_filelike_obj, 'w+b') IOError: [Errno 22] invalid mode ('w+b') or filename: u' C:\\Users\\LAP77\\JR\\Veg_Hidro_Final\\Excel\\Veg_Hidro_Final.xls'  Failed to execute (TableToExcel).  

 

and when I use the script again, this is the error.

Runtime error  Traceback (most recent call last):   File "<string>", line 14, in <module>   File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 6966, in MakeTableView     raise e ExecuteError: ERROR 000725: Table Name: Dataset tview already exists.  
0 Kudos
by Anonymous User
Not applicable

Ok, Like Dan said earlier you have spaces in your paths. r" C:...".

This error is saying it cant find <space>C:/... in your output.  remove the spaces and try again.

0 Kudos
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.

JoelRodriguez95
New Contributor II

it was finally achieved! the scripts is this:

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)
0 Kudos
DanPatterson
MVP Esteemed Contributor

print the featureclass name and the out_xls name ... ie print(fc, out_xls)


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

I did it in my last answer, thanks for your time

0 Kudos