I have a script that creates a excel file but i need it to delete the "OID" field prior to exporting to excel, is this possible or does it have to be done after the export?
I have tried the following, it runs fine and i don't get error.
# Process: Make Feature Layer (2)
arcpy.MakeFeatureLayer_management("In_memory\Blah", "In_memory\Blah3")
#fields = arcpy.ListFields("In_memory\Blah3", "OID")
#if len(fields) != 1:
# arcpy.DeleteField_management("OID")
fields = [f.name for f in arcpy.ListFields("In_memory\Blah3")]
for i,f in enumerate(fields):
if f == 'OID':
del fields
# Process: Table Select
arcpy.TableToExcel_conversion("In_memory\Blah3",Listing_xls)
Solved! Go to Solution.
If you are using mail merge it will hurt nothing to leave the OID field in place.
Something like this might work.
# Get list of usable field names (exclude OID and SHAPE fields) fieldNames = [ f.name for f in arcpy.ListFields(inputTable) if f.type != "Geometry" and f.editable == True ]
so you are saying switching to a gdb increases the number of fields you need to delete?? excel macro time
Yes when changing to a geodatabase and exporting it exports the area and shape fields.
I was using a macro but i got tired of having to open the export and run the macro.
fc = "C:/Temp/Default.gdb/Blah"
desc = arcpy.Describe(fc)
flds = [fld.name for fld in desc.fields]
flds_remove = [desc.OIDFieldName,
desc.shapeFieldName,
desc.areaFieldName,
desc.lengthFieldName]
for fld in flds_remove:
flds.remove(fld)
arcpy.MakeQueryTable_management(fc, "tmp_view", "NO_KEY_FIELD", "", flds)
#Result 'tmp_view'
arcpy.TableToExcel_conversion("tmp_view", r"C:/Temp/fc_dump.xls")
#Result 'C:/tmp/fc_dump.xls'
arcpy.Delete_management("tmp_view")If you want it to work with tables as well as feature classes, you would need to add some code to check for a shape field before adding it to flds_remove; otherwise, an error will be generated.
Worked. Thanks.
If my or someone else's answer addressed your issue, please mark it correct so others know it has been answered.