Export multiple tables in GDB to Excel with a new sheet for each table

5226
11
05-16-2018 09:05 AM
Brian_McLeer
Occasional Contributor II

Researching to see if there is a way to use Python to export multiple tables in a File Geodatabase to a single Excel file. I would like for each sheet name to have the same name as the table.

My current situation is that I ran a Python script to take all the domains a GDB and create a table within the GDB. 

The article referenced from:

https://community.esri.com/thread/163889 

def main(): 
 import arcpy, os 
 gdb = r"Z:\GIS_Projects\zUpdatesBrian\Scripts\ListDomains\ListDomains.gdb" 
 arcpy.env.overwriteOutput = True 
 
 for dom in arcpy.da.ListDomains(gdb): 
 if dom.domainType == 'CodedValue': 
 arcpy.DomainToTable_management(in_workspace=gdb, 
 domain_name=dom.name, 
 out_table=os.path.join(gdb, dom.name), 
 code_field="item", 
 description_field="descrip", 
 configuration_keyword="") 
 
 print " - domain '{0}' of type '{1}' exported to table".format(dom.name, dom.domainType) 
 else: 
 print " - domain '{0}' of type '{1}' skipped".format(dom.name, dom.domainType) 
 
if __name__ == '__main__': 
 main()
Brian
0 Kudos
11 Replies
Brian_McLeer
Occasional Contributor II

That really helps, thank you very much, Joe!

Brian
0 Kudos
mdavenport
New Contributor

Hi @JoeBorgione !  I hope you can help me. I'm trying to get multiple tables to one xlsx file. It worked once, but the tab names were a bit off.  I retried and now my code is not working but I'm not getting an error; it's just not writing the file. Any guesses on how to correct? I'm new to ArcPy so trying my best!  My ultimate goal is to have many tables, not just three, go into one large excel workbook. I thought I'd start there though.  I appreciate any feedback!

import pandas as pd
import arcpy
from arcpy import env
fields = ['FORESTNAME', 'SUM_Acres_1']
targetXL = r'D:/Projects/testtablemerge.xlsx'
arcpy.env.workspace = "D:/Projects/Workflow_python.gdb"
tables = arcpy.ListTables()
writer = pd.ExcelWriter(targetXL)
for table in tables:
    if table == 'dmg_NF_intersect_SummarizeAcres':
        pass
    elif table == 'dmg_NF_MPB_all':
        pass
    elif table == 'dmg_NF_MPB_5N':
        pass
    else:
        inTable = table
        tableArray = arcpy.da.TableToNumPyArray(inTable,fields)
        df = pd.DataFrame(tableArray)
        df.to_excel(writer,table)
        del tableArray
        del df
        writer.save()

 

0 Kudos