Exporting selected attributes to excel

382
6
09-10-2021 08:37 AM
2Quiker
Occasional Contributor II

I need to be able to export the selected attributes from Arcmap. I have the following but I get an error on line 32. The first arcpy.Addmessage does print the OID's but not the second Addmessage. My guess is that it's not creating the temp_view. How can I get past this error?

error on line 32

ERROR 000732: Input Table: Dataset tmp_view does not exist or is not supported
Failed to execute (TableToExcel)

selection = "NParcels"

try:
    if int(arcpy.GetCount_management(selection).getOutput(0)) > 0:
        
        arcpy.CopyRows_management(selection, "selct")
        arcpy.MakeTableView_management("selct","selectionView")

        fc = "selectionView"
        desc = arcpy.Describe(fc)
        oidList = [oid[0] for oid in arcpy.da.SearchCursor(fc,["OID@"])]
        arcpy.AddMessage(oidList)
        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)
        oidList = [oid[0] for oid in arcpy.da.SearchCursor(fc,["OID@"])]
        arcpy.AddMessage(oidList)
except:
    pass

wp1 = "C:/Temp"
outputName = arcpy.GetParameterAsText(0)
outputDist = arcpy.GetParameterAsText(1)
OutputExt = ".xls"
#Result 'tmp_view'  

arcpy.TableToExcel_conversion("tmp_view", wp1 +'\\'+ outputName + '_' + outputDist + '_' + 'Notification' + OutputExt)

K

0 Kudos
6 Replies
DavidPike
MVP Frequent Contributor

I'd as some exception handling to your except as it's very likely that something is wrong in the try block, the except does nothing and then tmp_view is called when it doesnt exist.

 

Personally I would get rid of the try except and any arcpy.getparameter stuff until you're sure the code works.

0 Kudos
2Quiker
Occasional Contributor II

I removed the try block. I get the following error,

ERROR 000055: Cannot create a Query Table for this workspace
Failed to execute (MakeQueryTable).

0 Kudos
DavidPike
MVP Frequent Contributor

Just a guess from looking at the docs, but fields argument seems to require a list of lists, something like

[  [field1], [field2] ...  ]

maybe:

        flds = [[fld.name] for fld in desc.fields]
        flds_remove = [[desc.OIDFieldName],  
                      [desc.shapeFieldName],  
                      [desc.areaFieldName],
                      [desc.LengthFieldName]] 
0 Kudos
2Quiker
Occasional Contributor II

I did have the following but I kept giving me errors like the following, so I removed them.

[desc.shapeFieldName],
AttributeError: DescribeData: Method shapeFieldName does not exist

[desc.shapeFieldName],
AttributeError: DescribeData: Method areaFieldName does not exist

[desc.shapeFieldName],
AttributeError: DescribeData: Method desc.LengthFieldName does not exist

 

flds_remove = [desc.OIDFieldName,
desc.shapeFieldName,
desc.areaFieldName,
desc.LengthFieldName] 

 

 I am thinking that the arcpy.MakequeryTable_management is not working because I don't get a list of OID after but I am not sure why.

0 Kudos
2Quiker
Occasional Contributor II

I was able to get this to work but I had to us the arcpy.CopyFeatures_management which is ok I guess but I would like to skip this part if possible?

if int(arcpy.GetCount_management(selection).getOutput(0)) > 0:
    arcpy.MakeFeatureLayer_management(selection, "SelectLyr")
    arcpy.CopyFeatures_management("SelectLyr", "C:\Temp\Scratchworkspace.gdb\Tempselect")

fc = "C:\Temp\Scratchworkspace.gdb\Tempselect"
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)
file = folder = os.path.dirname(mxd.filePath)+ "/" + "tmp_view"
arcpy.MakeQueryTable_management(fc, "tmp_view", "NO_KEY_FIELD", "", flds)

wp1 = "C:/Temp"
outputName = arcpy.GetParameterAsText(0)
outputDist = arcpy.GetParameterAsText(1)
OutputExt = ".xls"
#Result 'tmp_view'  

arcpy.TableToExcel_conversion("tmp_view", wp1 +'\\'+ outputName + '_' + outputDist + '_' + 'Listing' + OutputExt)

 

0 Kudos
2Quiker
Occasional Contributor II

I am still trying how to figure out how do this without the Copymanagment.

I was able to do it with the arcpy.da.FeatureClassToNumPyArray but it exported all the fields I need to remove the extra fields like the OID, area and length fields.

I have the following but it's still including those extra fields. I get the following error.

DescribeData: Method lengthFieldName does not exist

 

selection = "NParcels"
if int(arcpy.GetCount_management(selection).getOutput(0)) > 0:
    arcpy.MakeTableView_management(selection, "NP_VIEW")

    fc1 ="NP_VIEW"
    dsc = arcpy.Describe(fc1)
    fields = dsc.fields
    out_fields = [dsc.OIDFieldName, dsc.lengthFieldName, dsc.areaFieldName]
    fieldnames = [field.name for field in fields if field.name not in out_fields]

    nparr = arcpy.da.FeatureClassToNumPyArray(fc1, fieldnames)
    arcpy.TableToExcel_conversion(fc1, "C:/temp/Test.xls")

 

0 Kudos