This error message occurs when your output folder doesn't exist. Check that C:\GIS\OC Sheriffs\Image\L2
exists on the different computer you are now running the script on. You can use os.path.isdir()
to test whether a folder exists.
outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files
# Check that the path exists
if os.path.isdir(outputFolder):
## the rest of your code goes here
else:
print "The folder doesn't exist!"
Since you're running ArcGIS 10.4 I would migrate the script from the old arcpy.SearchCursor()
to the more efficient arcpy.da.SearchCursor()
import arcpy, os
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Grid")[0]
outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files
# Check that the path exists
if os.path.isdir(outputFolder):
with arcpy.da.SearchCursor(lyr, ['PageName', 'SHAPE@']) as cursor:
for row in cursor:
df.extent = row[1].extent
outputFilePath = r"{}\{}.jpg".format(outputFolder, row[0])
arcpy.mapping.ExportToJPEG(mxd, outputFilePath, df, df_export_width = 1200, df_export_height = 1200, resolution = 96, world_file=True, jpeg_quality = 100)
else:
print "Folder {} doesn't exist!".format(outputFolder)
Have updated the script to add a extent buffer to add a bit of space around the current feature. This increases the extent scale by a specified percentage, then rounds that scale up to the next value that matches specified rounding factor (explained below the script)
import arcpy, os
def update_scale(df, extraScalePct, scaleRound):
exScale = df.scale # Current scale
newScale = exScale + (exScale * (float(1)/(float(100)/extraScalePct))) # Increase DF scale by specified amount
roundScale = (newScale + (scaleRound -1)) // scaleRound * scaleRound # Round the new scale up to the next value as specified
return roundScale
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "Lines")[0] ##shapefile layer name called grid
outputFolder = r"C:\GIS\OC Sheriffs\Image\L2" # Your Output folder path to store the exported JPG files
if os.path.isdir(outputFolder):
with arcpy.da.SearchCursor(lyr, ['PageName', 'SHAPE@']) as cursor:
for row in cursor:
print row[0]
df.extent = row[1].extent
df.scale = update_scale(df, 10, 250) # Increase scale by 10% then round up to next 250
outputFilePath = r"{}\{}.jpg".format(outputFolder, row[0])
arcpy.mapping.ExportToJPEG(mxd, outputFilePath, df, df_export_width = 1200, df_export_height = 1200, resolution = 96, world_file=True, jpeg_quality = 100)
else:
print "Folder {} doesn't exist!".format(outputFolder)
To set the extent buffer and the rounding, modify the line df.scale = update_scale(df, 10, 250)
. df
is your dataframe, 10
is the percentage to increase the scale (e.g. 1:250 becomes 1:275), 250
is the rounding factor that the new scale is rounded up to (e.g. 1:275 becomes 1:500). Modify these two values to suit.
C:\GIS\Data and Maps\2014MDCExport_CountyWide_localData.mxd
Is the path to your data correct? Can this other computer access the MXD?