1

I am using the following code (ArcGIS 10.3.1) to produce a batch of PDFs:

mxd = 'CURRENT'
cid = 2
rf = ur'D:\Δοκιμές\Tester'
gdb = rf+'\\Ant.gdb'
arcpy.env.workspace = gdb
mxd = arcpy.mapping.MapDocument(('CURRENT' if not mxd else mxd))
df = arcpy.mapping.ListDataFrames(mxd,'DF')[0]
for lyr in arcpy.mapping.ListLayers(mxd):
    lyr.visible = False
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
mxd.activeView = 'PAGE_LAYOUT'
for elm in arcpy.mapping.ListLayoutElements(mxd):
    if elm.type=='DATAFRAME_ELEMENT' and elm.name == 'DF':
        elm.elementPositionX = 1.5
        elm.elementPositionY = 28.2
        elm.elementWidth = 18
        elm.elementHeight = 25.7
        elm.scale = 500 if elm.scale<=500 else math.ceil(elm.scale/100)*100
with arcpy.da.SearchCursor('Parcels',['pid','cid'],'cid = '+str(cid)) as cursor:
    for cur in cursor:
        arcpy.MakeFeatureLayer_management('Parts','Temp_'+str(cur[0]),'pid = '+str(cur[0]))
        lyr = arcpy.mapping.Layer('Temp_'+str(cur[0]))
        arcpy.mapping.AddLayer(df,lyr)
        df.extent = lyr.getExtent(True)
        df.scale = 500 if df.scale<=500 else math.ceil(df.scale/100)*100
        arcpy.RefreshActiveView()
        arcpy.mapping.ExportToPDF(mxd,rf+'\\'+str(cur[0])+'.pdf')
        arcpy.mapping.RemoveLayer(df,lyr)
        arcpy.Delete_management('Temp_'+str(cur[0]))
        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()
del mxd  

I define the mxd whether it is the currently opened or with an absolute path like (Line 1):

mxd = ur'D:\Test\test.mxd'  

When I use the 'CURRENT' option, I get the result as it is displayed on the first picture and when I use an absolute path for mxd, the result is, as it is displayed on the second picture:
MXD:'CURRENT'MXD:ur'D:\Tester'
It seems that when I use the absolute path for the mxd, the temporary Layers ('Temp_*'), the code creates, remain in the mxd and are printed with the wanted one.
Regarding the fact that I would like to be able to use the absolute path for the mxd, if possible, does anyone know why is this happening and probably a solution on how to solve it?

1 Answer 1

1

If you're not working from within an open mxd (with the current keyword), You must save the mxd at the end of the script, otherwise changes will not be permanent:

mxd.save()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.