Here's a different way to do this by setting the dataframe equal to the current layer's extent. It a lot more simple. Also, you'd better check your path because it shows the MXD being inside the geodatabase.
import arcpy
import os
path = "C:\\Users\\Et\\Documents\\ArcGIS" #need to use two backslashes
mxd = arcpy.mapping.MapDocument(os.path.join(path, "WinterGritting.mxd")) #use os.path.join
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_list = arcpy.mapping.ListLayers(mxd, "", df) #don't use indexing here
for layer in layer_list:
layer_name = str(layer.name)
for layer_check in layer_list: #turns on/off other layers
if layer_check != layer:
layer_check.visible = False
layer.visible = True
mxd.title = layer_name
layer_extent = layer.getExtent() #get the extent of the current layer
df.extent = layer_extent #set the dataframe to the layer's extent
arcpy.RefreshActiveView()
arcpy.mapping.ExportToJPEG(mxd, os.path.join(path, "Routes", layer_name + ".jpg"))
del mxd
Updated for comment.
Add the names of whatever layers you don't want to turn off into the landbase list.
import arcpy
import os
path = "C:\\Users\\Et\\Documents\\ArcGIS" #need to use two backslashes
mxd = arcpy.mapping.MapDocument(os.path.join(path, "WinterGritting.mxd")) #use os.path.join
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_list = arcpy.mapping.ListLayers(mxd, "", df) #don't use indexing here
landbase = ["Roads", "Lakes"] #add layer names you want always visible here
for layer in layer_list:
layer_name = str(layer.name)
if layer_name not in landbase:
for layer_check in layer_list: #turns on/off other layers
if layer_check != layer and layer_check.name not in landbase:
layer_check.visible = False
else:
layer_check.visible = True
mxd.title = layer_name
layer_extent = layer.getExtent() #get the extent of the current layer
df.extent = layer_extent #set the dataframe to the layer's extent
arcpy.RefreshActiveView()
arcpy.mapping.ExportToJPEG(mxd, os.path.join(path, "Routes", layer_name + ".jpg"))
del mxd
I see a couple potential errors/improvements. One is that your very last line should be indented one block so that it occurs in the for loop, but maybe that's just a formatting issue on this post, and your actual code is fine.
Issue 2 is what the error message is referring to. When calculating text fields, the value needs to be encapsulated in double quotes, like so -
arcpy.CalculateField_management (fc, field, '"' + fc + '"')
so you use single quotes as python string delimiters to encapsulate a single double quote on either side of the text value, and then concatenate it all together.
Another issue is that if you're adding a text field, you need to specify a field_length parameter, or at least I think you do. Maybe it defaults to something if you don't specify it, but I always add it. So your add_field line would be
arcpy.AddField_management (fc, field, "TEXT", field_length=50)
change the length as you see fit, depending on the length of text you intend to put in there.
Fourth- your 'field' variable is defined in a for block. That's not necessary, as you're always assigning it the value "RouteName". Plus, you then use it outside of that for block, and inside another for block. Change it like this -
field = "RouteName"
for fc in fc_tables:
arcpy.AddField_management (fc, field, "TEXT", field_length=50)
fifth - no need to loop through the list twice. You can do both operations on the feature class one after another, so your code would look like this -
field = "RouteName"
for fc in fc_tables:
arcpy.AddField_management (fc, field, "TEXT", field_length=50)
# No need to specify "PYTHON" here since you're not using it, and no need to cast fc to a string since it already is
arcpy.CalculateField_management (fc, field, '"' + fc + '"')
Best Answer
Based on your variable, you just need to make sure variables and strings are not confused
note that if you have the strings as variables without the "!", it is nicer to use format()