Export Attributes from Arcmap without Geometry fields

667
14
11-01-2019 09:57 AM
2Quiker
Occasional Contributor II

I need to export attributes from a certain layer in Arcmap but without the OID & Geometry fields to excel.

I've tried the following bet i am getting error ("AttributeError: DescribeData: Method areaFieldName does not exist") on line 15. I would appreciate any help with the code, thanks.

 

import arcpy
import os

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Notified")[0]

arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)

fc = "Notified"
desc = arcpy.Describe(fc)
f = desc.fields
fields = [f.name for f in f if f.name != desc.OIDFieldName and f.type != 'Geometry' and f.name != desc.areaFieldName and f.name != desc.lengthFieldName]
for fld in fields:  
    fields.remove(fields)

arcpy.MakeQueryTable_management(fc, "tmp_table", "NO_KEY_FIELD")‍‍‍‍‍‍‍‍
arcpy.TableToExcel_conversion("tmp_table", Notified_xls) ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
14 Replies
JakeSkinner
Esri Esteemed Contributor

I would recommend executing the Table to Table tool and export the layer to a File Geodatabase.  You can remove the OID using the Field Mapping paramter (the geometry should already be removed).  Then execute the Table to Excel tool on the geodatabase table.  After executing each of these, you can copy the python commands and paste them into your script.

2Quiker
Occasional Contributor II

Jake,

Thanks for the recommendation but I would have thought that just dropping or removing the fields would be easier.

I didn't want to do the field mapping.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

In your code you are removing the fields from the list, but you are not applying the fields list to any function afterwards.  I believe you will have to do some field mapping.  Here is an example that I was able to get to work:

import arcpy

# Set data path
intable = r"C:\temp\python\test.gdb\airports"

# Create a fieldinfo object
fieldInfo = arcpy.FieldInfo()

# Get the fields from the input
fields= arcpy.ListFields(intable)
for field in fields:
    if field.type != 'OID' and field.type != 'Shape':
       fieldInfo.addField(field.name, field.name, "VISIBLE", "")
    if field.type == 'OID':
       fieldInfo.addField(field.name, field.name, "HIDDEN", "")

# Create table view
arcpy.MakeTableView_management(intable, "airports_view", "", "", fieldInfo)

# Export to Excel
arcpy.conversion.TableToExcel("airports_view", r"C:\TEMP\Python\airports.xlsx")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

For some reason the OID is always added, so I had to add this as HIDDEN.

2Quiker
Occasional Contributor II

I am still have the the FID, Shape_Lenth & Shape_Area fields after the export to Excel after using what you posted

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Try setting those fields to HIDDEN as well.  Ex:

if field.type == 'FID':
       fieldInfo.addField(field.name, field.name, "HIDDEN", "")

If this still does not resolve the issue, can you upload a sample of your data?

2Quiker
Occasional Contributor II

I tried this again but i am not getting an error.

The Export_Output layer is a shapefile and the fields that are in the layer that i don't need are

Shape*, OBJECTID, Shape_Leng & Shape_Area

Traceback (most recent call last):
  File "c:\program files (x86)\arcgis\desktop10.6\ArcToolbox\Scripts\TableToExcel.py", line 224, in <module>
    arcpy.GetParameter(3))
  File "c:\program files (x86)\arcgis\desktop10.6\ArcToolbox\Scripts\TableToExcel.py", line 194, in table_to_excel
    for row in cursor:
RuntimeError: Cannot find field ''

Failed to execute (TableToExcel).

import arcpy

arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "Export_Output")[0]

#Exports NotifiedLots tabel to excel table
intable = lyr  
# Create a fieldinfo object
fieldInfo = arcpy.FieldInfo()

# Get the fields from the input
fields= arcpy.ListFields(intable)
for field in fields:
    if field.type != 'OID' and field.type != 'Shape':
       fieldInfo.addField(field.name, field.name, "VISIBLE", "")
    if field.type == 'OID':
       fieldInfo.addField(field.name, field.name, "HIDDEN", "")

# Create table view
arcpy.MakeTableView_management(intable, "Notif_view", "", "", fieldInfo)

# Export to Excel
arcpy.conversion.TableToExcel("Notif_view", r"C:\TEMP\Notif_view.xls")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JakeSkinner
Esri Esteemed Contributor

2 Quiker‌ can you zip and upload your shapefile?  Make sure you include the .shp, .shx, .dbf, and .prj in the zip file.

0 Kudos
2Quiker
Occasional Contributor II

I have attached a sample of the data.

Thanks for your help!

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I created a tuple called hiddenFields, and added all the fields I did not want to show in that.  Try the following:

import arcpy
arcpy.env.overwriteOutput = 1

# Set data path
intable = r"C:\TEMP\PYTHON\UserData\Export_Output.shp"

# Create a fieldinfo object
fieldInfo = arcpy.FieldInfo()

# Fields to Hide
hiddenFields = ('FID', 'Shape', 'OBJECTID', 'Shape_Leng', 'Shape_Area')

# Get the fields from the input
fields= arcpy.ListFields(intable)
for field in fields:
    if field.name not in hiddenFields:
       fieldInfo.addField(field.name, field.name, "VISIBLE", "")
    else:
       fieldInfo.addField(field.name, field.name, "HIDDEN", "")


# Create table view
arcpy.MakeTableView_management(intable, "export_view", "", "", fieldInfo)

# Export to Excel
arcpy.conversion.TableToExcel("export_view", r"C:\TEMP\Python\UserData\export.xlsx")
0 Kudos