import arcpy from datetime import datetime import PyPDF2 # Allow overwriting of output arcpy.env.overwriteOutput = True class ToolValidator: """Class to add custom behavior and properties to the tool and tool parameters.""" def __init__(self): """Initialize the ToolValidator class.""" self.params = arcpy.GetParameterInfo() # Set self.params for use in other functions def initializeParameters(self): """Initialize parameters.""" try: # Define REST URLs for the main layer and related table main_layer_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/0" related_table_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/1" # Set the selected event ID parameter to populate dropdown sel_event_id = self.params[0] sel_event_id.filter.list = [row[0] for row in arcpy.da.SearchCursor(main_layer_url, "event_id")] # Print all event IDs retrieved from the hosted feature layer event_ids = [row[0] for row in arcpy.da.SearchCursor(main_layer_url, "event_id")] for event_id in event_ids: arcpy.AddMessage("Retrieved Event ID: {}".format(event_id)) # Print the datasource for the hosted feature layer lyr = arcpy.MakeFeatureLayer_management(main_layer_url, "Main_Layer")[0] arcpy.AddMessage("Connected Hosted Feature Layer: {}".format(lyr.dataSource)) # Print the datasource for the related table tbl = arcpy.MakeTableView_management(related_table_url, "Related_Table")[0] arcpy.AddMessage("Connected Related Table: {}".format(tbl.dataSource)) except Exception as e: arcpy.AddError("An error occurred during parameter initialization: {}".format(str(e))) return def updateParameters(self): """Update parameters.""" return def updateMessages(self): """Update messages for the parameters.""" return class ToolExecutor: """Class to execute the tool functionality.""" def execute(self, parameters, messages): """Execute the tool.""" try: # Create ArcGIS Project object aprx = arcpy.mp.ArcGISProject("CURRENT") # Get the selected event ID from the parameter sel_event_id = parameters[0].valueAsText # Construct SQL expression based on the user input sql_expression_lyr = "event_id = '{}'".format(sel_event_id) sql_expression_tbl = "get_event_id = '{}'".format(sel_event_id) # Make selections on the layer and table based on the user input main_layer = arcpy.MakeFeatureLayer_management(main_layer_url, "Main_Layer")[0] arcpy.SelectLayerByAttribute_management(main_layer, "NEW_SELECTION", sql_expression_lyr) # Export the report with the layer selection set rpt_template_cover = aprx.listReports("Cover-Page-MNRWA1v1-Master-Report")[0] output_cover_file = "C:\\Reports\\CoverPage__MNRWA1v1_{}.pdf".format(datetime.now().strftime('%Y_%m_%d_%H%M_%S_hrs')) # Construct filename with current time arcpy.ExportReportToPDF_management(rpt_template_cover, output_cover_file, sql_expression_lyr) arcpy.AddMessage("Report for hosted feature layer has been generated and saved to: {}".format(output_cover_file)) # Export the report with the table selection set related_table = arcpy.MakeTableView_management(related_table_url, "Related_Table")[0] arcpy.SelectLayerByAttribute_management(related_table, "NEW_SELECTION", sql_expression_tbl) rpt_template_details = aprx.listReports("Detail-Pages-MNRWA1v1-Master-Report")[0] output_details_file = "C:\\Reports\\Details_MNRWA1v1_{}.pdf".format(datetime.now().strftime('%Y_%m_%d_%H%M_%S_hrs')) # Construct filename with current time arcpy.ExportReportToPDF_management(rpt_template_details, output_details_file, sql_expression_tbl) arcpy.AddMessage("Report for related table has been generated and saved to: {}".format(output_details_file)) # Combine the cover and detail reports into one combined_output_file = "C:\\Reports\\Combined_MNRWA1v1_{}.pdf".format(datetime.now().strftime('%Y_%m_%d_%H%M_%S_hrs')) with open(output_cover_file, 'rb') as cover_file, open(output_details_file, 'rb') as details_file: merger = PyPDF2.PdfFileMerger() merger.append(cover_file) merger.append(details_file) merger.write(combined_output_file) arcpy.AddMessage("Combined Report has been generated and saved to: {}".format(combined_output_file)) except Exception as e: arcpy.AddError("An error occurred: {}".format(str(e))) # REST URLs for the main layer and related table main_layer_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/0" related_table_url = "https://services8.arcgis.com/CQeG4yN4mcOJaKOV/arcgis/rest/services/service_b4b3e49de39547628b85a73989096909/FeatureServer/1" # Execute tool if __name__ == "__main__": validator = ToolValidator() executor = ToolExecutor() executor.execute(validator.params, arcpy.GetMessageCount())