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)
Jake,
I get error on in TableToExcel raise e ExecuteError: ERROR 000814: Invalid file type Failed to execute (TableToExcel) with ".xlsx
"
If i change the output to ".xls" get "RuntimeError: Cannot find field ''.
I need this to run in side python in Arcmap not in a stand alone script. This will be made into a tool script.
import arcpy
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.ListLayers(mxd, "Export_Output")[0]
# 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(lyr)
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(lyr, "Notif_view", "", "", fieldInfo)
# Export to Excel
arcpy.conversion.TableToExcel("Notif_view", r"C:\temp\Notif_view.xlsx")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
I shy away from field mapping as well. If you don't mind a hack, you could do a table to excel and then open your new excel worksheet and just delete the fields you don't want.....
Ya I agree i could just open the excel spreadsheet and delete them if i were only doing it so often. I usually run this process about 10 times a week. So i was trying to make it as stream lined as possible.
Has anyone tried the editable property? aka, only export those that are editable in conjunction with some of the others.
editable (Read and Write) | The editable state: True if the field is editable. | Boolean |
A working possibility could be to convert to numpy array, then use pandas to convert the array to excel?
import arcpy
import pandas as pd
arcpy.env.overwriteOutput = True
inTable = r"yourpath\Export_Output.shp"
outTable1 = r"yourpath\export.xls"
fieldNames = ["OBJECTID", "PARCEL_NO", "OwnerName", "Address", "City", "State", "ZipCode"]
arr = arcpy.da.FeatureClassToNumPyArray(inTable, fieldNames)
df = pd.DataFrame(arr)
df1 = df.to_excel(outTable1, index=False)