1

I have several folders which each contain a GDB.

Now, I export the attribute tables of polygon feature classes using the following code (probably could use some optimization):

folder = arcpy.GetParameterAsText(0)

for dirpath, dirnames, filenames in arcpy.da.Walk(folder, datatype="FeatureClass",type="Polygon"):
    for filename in filenames:
        fc = os.path.join(dirpath, filename)
        table = dirpath + os.sep + filename + "_Table"
        result = arcpy.GetCount_management(fc)
        if int(result.getOutput(0)) > 0:
            print "Creating " + table
            arcpy.CopyRows_management(fc, table)

Output looks like:

-folder
--subfolder1
---*.gdb <--contains multiple tables
--subfolder2
---*.gdb <--contains multiple tables

I would like to simply output an excel version of the table at the same time, but i am unsure how to parse out the folder containing the .gdb to use as the path for the excel file.

Outout should then look like:

-folder
--subfolder1
---*.gdb <--contains multiple tables
---Excel1
---Excel2
---etc.
--subfolder2
---*.gdb <--contains multiple tables
---Excel1
---Excel2
---etc.

How would I go about getting that parameter to output the Excel file in the correct location?

2

CopyRows can't output to Excel, it can only output to dBASE, ArcSDE geodatabase, file geodatabase, personal geodatabase or INFO tables. You need to use the TableToExcel_conversion tool.

Then you need to use os.path.dirname to get the gdb parent directory.

Something like (untested...):

import os, arcpy
folder = arcpy.GetParameterAsText(0)

for dirpath, dirnames, filenames in arcpy.da.Walk(folder, datatype="FeatureClass",type="Polygon"):
    for filename in filenames:
        fc = os.path.join(dirpath, filename)
        xls = os.path.join(os.path.dirname(dirpath), filename + "_Table.xls")
        result = arcpy.GetCount_management(fc)
        if int(result.getOutput(0)) > 0:
            print "Creating " + xls
            result = arcpy.TableToExcel_conversion(fc, xls)
1

Below your 'CopyRows' command, where you're outputting tables inside the gdb, you need to output essentially to a level 'up', as indicated in your diagram...do this with a simple os.path.dirname(dirpath). (Make sure you've imported the os module.)

FYI, to be consistent you should use os.path.join similarly (instead of '+ os.sep +'):

os.path.join(dirpath, filename + "_Table")

0

This ended up being really easy after some effort in ModelBuilder. Just feed it a folder and have your iterator run recursively and it will run thru any number of gdbs in the folder and output excel files for each feature class in each gdb.

ExcelExporter

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.