I'm am creating a script that will allow a user to add XY coordinates to a point layer, then export (along with any addition user selected fields) to Excel. I am getting stuck on the iteration of the selected fields and how to map them to prepare to export to Excel. I have a parameter that derives the field names from the input point layer and the user can tick each field they wish to include in the Excel export.
3 Answers
I found that I simply needed to split the field selections and then iterate through the list using field mapping.
paramInWells = arcpy.GetParameterAsText(0)
paramInFields = arcpy.GetParameterAsText(1)
paramInFieldList = paramInFields.split(";")
paramOutExcel = arcpy.GetParameterAsText(2)
arcpy.SetProgressorLabel("Processing...")
arcpy.AddMessage("\nAdding XY coordinates")
fms = arcpy.FieldMappings()
for field in paramInFieldList:
fieldName = arcpy.FieldMap()
fieldName.addInputField(paramInWells, field)
fms.addFieldMap(fieldName)
arcpy.FeatureClassToFeatureClass_conversion(paramInWells, "in_memory", "XYCoords_Tmp", field_mapping=fms)
paramOutXyTemp = "in_memory\\XYCoords_Tmp"
arcpy.AddXY_management(paramOutXyTemp)
arcpy.AddMessage("\nCreating Excel document")
arcpy.TableToExcel_conversion(paramOutXyTemp, paramOutExcel)
One option you could try:
- Make feature layer of your source field layer
- Turn off fields or make them HIDDEN via the
fieldInfo.setVisible()
Method sourcing from the user defined parameter.
Turning fields on and off using arcpy
- Finally, you may use the Table to Excel method for creating the excel table from the feature layer as the input table
you could also use the built in tool "export feature attribute to ascii". This is ascii file and not excel, but very easy to import in excel. It adds XY coordinates automatically and you can check the fields that you want to keep.