Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try to export multi MXD into pdf files, but i want to exclude specific MXD files from this action:

import arcpy,os
import arcpy.mapping
from arcpy import env

env.workspace = r"D:\desktop\Project"
for mxd in arcpy.ListFiles("*.mxd"):
    mapdoc = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxd)
    if 'project2' or 'project3' in mxd:
        pass
    arcpy.mapping.ExportToPDF(mapdoc, r"D:\desktop\Project\out\\"+os.path.basename(mapdoc.filePath).replace ('.mxd','') +".pdf")
    print mxd
    mapdoc.save() 
del mxd

But the result is that all MXD files has been exported:

 >>> 
project.mxd
project2.mxd
project3.mxd
>>>   
share|improve this question
    
While both answers are technically correct, they differ in which mxd's that are exported: those which contains project2 or project3, or those that doesn't. – Martin Jan 25 at 9:39
    
Strictly speaking this is a Python rather than ArcPy issue because it is really about constructing if/else and using pass statements. I'm just saying this to try and help you see that when you write a Python script that imports ArcPy there will be some parts better researched at Stack Overflow. – PolyGeo Jan 25 at 9:42
1  
@Martin, good point. I've updated my answer to cover both cases – Alex Tereshenkov Jan 25 at 9:45
up vote 5 down vote accepted

Your problem is that you use pass which will do nothing, but jumps to the next code row.

Do it like this:

for mxd in arcpy.ListFiles("*.mxd"):
    mapdoc = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxd)
    if 'project2' not in mxd and 'project3' not in mxd:
      arcpy.mapping.ExportToPDF(mapdoc, r"D:\desktop\Project\out\\"+os.path.basename(mapdoc.filePath).replace ('.mxd','') +".pdf")
      print mxd
      mapdoc.save() 
del mxd

This will export all mxds except those which contain the specified strings. If you need to export only project2 and 3, then just remove not in and use or, so it will be:

if 'project2' in mxd or 'project3' in mxd:

It is also important to understand the syntax of evaluation in Python. When you wrote

if 'project2' or 'project3' in mxd:

This will always evaluate to True because 'project2' is not an empty string (empty strings will evaluate to False). Look here for details.

Instead, you should always complete the evaluation by providing the right part of the check: if 'project2' in mxd and 'project3' in mxd.

If you have many mxd names to check, you might end up having very long conditions. In this cases, you could use any.

mxds_to_skip = ["project1","project2","project3","project4"]
print any(k in "project3" for k in mxds_to_skip)
>>> True
print any(k in "project7" for k in mxds_to_skip)
>>> False
share|improve this answer
1  
The pass statement is one issue. The other main issue with the OP's code is that if 'project2' will always evaluate to True. I know your code fixes this, but your answer would be improved if you explained why. – Luke Jan 25 at 10:00
1  
@Luke, thanks, great point. Done! – Alex Tereshenkov Jan 25 at 10:38

I want to come at this from a different direction and show you how you can test the logic in each of the answers by using purely Python rather than ArcPy. I may or may not have copied/understood each of their logics but I don't think that matters because what I am trying to show is how you can test them, and in particular experiment with yours.

mxdList = ["project.mxd","project2.mxd","project3.mxd"]

print "Your coding pattern"
for mxd in mxdList:
        if 'project2' or 'project3' in mxd:
            pass
        print mxd

print "AlexTereshenkov coding pattern 1"
for mxd in mxdList:
    if 'project2' not in mxd and 'project3' not in mxd:
      print mxd

print "AlexTereshenkov coding pattern 2"
for mxd in mxdList:
    if 'project2' in mxd or 'project3' in mxd:
      print mxd

print "Midavalo coding pattern"
for mxd in mxdList:
    if 'project2' or 'project3' in mxd:
        pass
    else:
      print mxd

print "Stefan coding pattern"
for mxd in mxdList:
    if 'project2' or 'project3' in mxd:
        print mxd


>>> ================================ RESTART ================================
>>> 
Your coding pattern
project.mxd
project2.mxd
project3.mxd
AlexTereshenkov coding pattern 1
project.mxd
AlexTereshenkov coding pattern 2
project2.mxd
project3.mxd
Midavalo coding pattern
Stefan coding pattern
project.mxd
project2.mxd
project3.mxd
>>> 
share|improve this answer
1  
Good answer, being along the lines of teaching to fish! – Martin Jan 25 at 10:06
1  
Thanks @Martin When I was new to Python and arcgisscripting/ArcPy I think it was when I began to understand where they started and pure Python ended that made a lot of things fall into place. You may want to look in on meta.gis.stackexchange.com/questions/4058/… - if you have not already. – PolyGeo Jan 25 at 10:11

Try putting your export into an else

for mxd in arcpy.ListFiles("*.mxd"):
    mapdoc = arcpy.mapping.MapDocument(r"D:\desktop\Project\\" + mxd)
    if 'project2' in mxd or 'project3' in mxd:
        pass
    else:
        arcpy.mapping.ExportToPDF(mapdoc, r"D:\desktop\Project\out\\"+os.path.basename(mapdoc.filePath).replace ('.mxd','') +".pdf") 
        print mxd 
        #mapdoc.save()

Your if doesn't excluded files as the else is missing.

EDIT: Updated to correct the if statement that I missed last night. Also I don't think the mapdoc.save() is required here, unless you need to resave your mxd.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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