1

I would like to write only specific fields to an excel export. My current script is written as this:

fc = r"C:/Users/maps/table.xls"
xlsLocation = r"C:/Users/maps"
xlsOutputName = "SanFrancisco"
xlsPath = xlsLocation + '\\' + xlsOutputName + '.xls'
keepFields = ["name", "UID", "notes", "records", "surveyname"]

# I wrote the following, but it returned error 999999: Error executing function. I assume this maybe because the listfield function can't read xls?

fields = arcpy.ListFields(xlsPath)
dropFields = [x.name for x in fields if x.name not in keepFields]
print "Deleting extraneous fields in xls"
arcpy.DeleteField_management (xlsPath, dropFields)

Not certain about how to proceed. Our projects requires xls, so no other format can be considered. The excel file contains over 50 fields. I am using python 2.7

  • I'd start with Excel To Table For this question recording your version of ArcGIS will be important. – PolyGeo Mar 8 '17 at 4:11
  • Beware your case! if x.name not in keepFields should be either upper() or lower() - python is a case sensitive language, name is not the same as Name. Rather than delete field make a feature layer and use a field_info object to restrict the fields you don't want resources.arcgis.com/en/help/main/10.2/index.html#//…. Your error is probably because you're trying to remove a required field (like OID or Shape). – Michael Stimson Mar 8 '17 at 4:19
  • 1
    I think problem is that you are trying to treat an XLS as a table. Try using Delete Field from its tool dialog to delete a field from an XLS direct. I don't think you will be able to. I would not expect ListFields to be able to read its fields either. – PolyGeo Mar 8 '17 at 4:28
1

You didn't tell us anything about your feature-layers (where the data is coming from). So far, it seems you are merely trying to manipulate an xls file via arcpy.

You cant use ListFields on a xls. Use it on your feature-layer or geodatabase table, then sort out the fields you don't need, and THEN export to xls. Depending on your ArcMap version you may find the Table to Excel Tool in your conversion toolbox. Otherwise download it first.

You are trying to remove the fields from an xls, but you need to do that when you export the table from your feature-layer/shapefile, and not when the xls is already written.

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.