Wednesday 5 July 2017

arcpy - Can MakeFeatureLayer Object be Passed into ExportReport Function Layer Parameter?


I'm trying to pass an in memory layer object created from a feature class using Code:


arcpy.MakeFeatureLayer_management()

into the Code:


arcpy.mapping.ExportReport()

function. It's erroring out, so I think it's having trouble with the in memory feature layer (Does a feature layer have a.lyr extension?).


My code is:


Code:



arcpy.MakeFeatureLayer_management("SOILFC", "soil.lyr")
arcpy.mapping.ExportReport("soil.lyr", reportfilename, r"C:\Test_Outputs\ProjectReport1.xls")

I'm not sure if the mapping class needs an actual map document to work on, but I'm hoping not. I don't want to go through the process of creating a temp .mxd, setting a dataframe reference and then add in some layers somewhere just for the purpose of getting a layer object to create a report. I was just hoping I could create the report using in memory objects because the only thing I care about if the .xls report from the final output.



Answer



arcpy.management.MakeFeatureLayer doesn't make a layer file, it makes a layer in memory. ExportReport requires a layer object. Combining the two, you can do this:


# Make the layer
arcpy.management.MakeFeatureLayer("SOILFC", "soil_layer")

# Get reference to layer

lyr = arcpy.mapping.Layer("soil_layer")

# Do the magic part
arcpy.mapping.ExportReport(lyr, reportfilename, r"C:\Test_Outputs\ProjectReport1.xls")

# Remove layer from TOC and memory
del lyr
arcpy.management.Delete("soil_layer")

The missing part in your script is getting a layer object from the newly created Feature Layer.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...