0

I am using ArcPy with ArcMap 10.5.

I have a written a script that reads an Excel file, plots the X,Y points, creates heatmap rasters, and prints them to PDF. The broken part is that the PDF is overwriting itself rather than producing 10 separate files. The resulting name of the pdf is "District_%d_%s", rather than "District 1".

This is the part of the script that is problematic:

for i in range(1, 11):
    print("\tstarting with council district %s" % i)
    my_clause = 'COUNCIL_DISTRICT = %s' % i
    fill_clause = 'COUNCIL_DISTRICT <> %s' % i
    title_text = "Council District %s" % i
    raster_name = "kden_d%s" % i
    pdf_name = "District_%d_%s.pdf"
    #-----------REFRESH TEMPLATE------------------
    for lyr in arcpy.mapping.ListLayers(mxd, "", df): #set visibility and remove older layers
        lyr.visible = False
        if lyr.name in visibleMapLayers:
            lyr.visible = True
        if lyr.name == "District_Fills_SRC":  
            lyr.definitionQuery = fill_clause
        if lyr.name == "District_Outlines_SRC":  
            lyr.definitionQuery = my_clause
        if lyr.name == "case_density":
            lyr.replaceDataSource(autoLayerGDB, "FILEGDB_WORKSPACE", raster_name)

    selection_subset = arcpy.MakeFeatureLayer_management(output_layer, scratch_output, where_clause=my_clause)


    subset_count = format( int( arcpy.GetCount_management(selection_subset).getOutput(0) ), "," )
    # description_text = 'This map examines the density of Code Cases opened in District %s during Fiscal Year %s (and ending %s). The total number opened in this District was %s.' % ( i , _fy , end_date, subset_count )
    description_text = 'This map examines the density of Code Cases opened in District %s'

    arcpy.MakeFeatureLayer_management(outline_lyr, "District Outline", where_clause=my_clause) # generate a feature layer and...
    addLayer_1 = arcpy.mapping.Layer("District Outline") #pass it as a layer...
    arcpy.mapping.AddLayer(df, addLayer_1, "AUTO_ARRANGE")  #to the AddLayer() function
    newLyr = arcpy.mapping.ListLayers(mxd, "District Outline", df)[0]
    arcpy.mapping.UpdateLayer(df, newLyr, outline_lyr, symbology_only = True) #apply symbology to new layer

    arcpy.SelectLayerByAttribute_management(addLayer_1,"NEW_SELECTION")  
    df.extent = addLayer_1.getSelectedExtent()  
    arcpy.SelectLayerByAttribute_management(addLayer_1,"CLEAR_SELECTION") 

    arcpy.MakeFeatureLayer_management(fill_lyr, "District Fill", where_clause=fill_clause) # generate a feature layer and...
    addLayer_2 = arcpy.mapping.Layer("District Fill") #pass it as a layer...
    arcpy.mapping.AddLayer(df, addLayer_2, "AUTO_ARRANGE")  #to the AddLayer() function
    arcpy.mapping.UpdateLayer(df, addLayer_2, fill_lyr, symbology_only = True) #apply symbology to new layer

    #----------Update Layout and Export----------|
    print("\tUpdating map text...")
    for elem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):#update the text boxes
        if elem.name == "which_district":
            elem.text = title_text
        if elem.name == "description":
            elem.text = description_text

    print("\tPDF generated: %s " % pdf_name)
    PDF_Output_Folder = r"\\coacd.org\dfs\SWS\Code Enforcement\ACD Admin Operations\GIS\Operations\Council District Analysis Reporting\Auto_HeatMap_WIP\Deliverable"
    arcpy.mapping.ExportToPDF(mxd, PDF_Output_Folder + pdf_name)
  • 1
    I've never used Python, but I would say that line with pdf_name definition should be pdf_name = "District_%d.pdf" % i – TomazicM Jun 20 at 22:19
2

Change:

pdf_name = "District_%d_%s.pdf"

To:

pdf_name = "District %s.pdf" % i
  • %s works fine, str(1) == "%s" % 1 – mikewatt Jun 20 at 22:38

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.