Select to view content in your preferred language

Almost there, but I need help finding a bug

2837
10
07-16-2013 04:04 PM
TomMagdaleno
Regular Contributor
Hi all,
  I'm having a problem with my code.  It creates data driven pages from two seperate MXDs as left and right side page PDFs.  Then it puts them together into one PDF.   It used to work on my Windows XP machine and ArcGIS 10.0 but is giving me trouble on my new Windows 7 machine with ArcGIS 10.1.  The code is below.  Most is commented out because the failure happens when it tries to combine the two sets of pdfs.  Any ideas?


import arcpy, os

# Create an output directory variable
#
outDir = r"C:\Working_Directory\Combined"  

# Create a new, empty pdf document in the specified output directory
#
finalpdf_filename = outDir + r"\FinalStorm.pdf"
if os.path.exists(finalpdf_filename):
  os.remove(finalpdf_filename)
finalPdf = arcpy.mapping.PDFDocumentCreate(finalpdf_filename)

# Add the title page to the pdf
#
# finalPdf.appendPages(r"G:\CamarilloGIS\Projects\Atlases\Storm_Drain\2007\CoverSheet.pdf")

# Add the index map to the pdf
#
#  finalPdf.appendPages(r"G:\CamarilloGIS\Projects\Atlases\Storm_Drain\2007\GridIndex.pdf")

# Create Facing Pages for the map book
# Create pages for left-hand side of the book
#
mxdPathLeft = r"G:\CamarilloGIS\Projects\PublicWorks\STORMWATER\Updates\Even11x17_Aerial.mxd"
tempMapLeft = arcpy.mapping.MapDocument(mxdPathLeft)
tempDDPLeft = tempMapLeft.dataDrivenPages

# Loop creates individual pdf's for odd numbered pages
#
#for pgNumLeft in range(1, tempDDPLeft.pageCount + 1,):
#  temp_filename = r"C:\Working_Directory\L_" + \
#                            str(pgNumLeft) + ".pdf"
#  if os.path.exists(temp_filename):
#    os.remove(temp_filename)
#  tempDDPLeft.exportToPDF(temp_filename, "RANGE", pgNumLeft)
#
# Create pages for right-hand side of the book
#
mxdPathRight = r"G:\CamarilloGIS\Projects\PublicWorks\STORMWATER\Updates\Odd11x17_Aerial.mxd"
tempMapRight = arcpy.mapping.MapDocument(mxdPathRight)
tempDDPRight = tempMapRight.dataDrivenPages

# Loop creates individual pdf's for even numbered pages
#
#for pgNumRight in range(2, tempDDPRight.pageCount + 1,):
#  temp_filename = r"C:\Working_Directory\R_" + \
#                             str(pgNumRight) + ".pdf"
#  if os.path.exists(temp_filename):
#    os.remove(temp_filename)
#  tempDDPRight.exportToPDF(temp_filename, "RANGE", pgNumRight)

# Append right and left-hand pages together in proper order
i = 1 #set i equal to 1 to start with the first PDFs
for pgNum in range(1, tempDDPLeft.pageCount + 1):
  print "Page", pgNum, "of", tempDDPLeft.pageCount
  tempPDF = r"C:\Working_Directory\combined" + os.sep + str(pgNum) + ".pdf"
  finalPdf.appendPages(r"C:\Working_Directory\L_" + str(i) + ".pdf") #appends the first PDF from the odds
  finalPdf.appendPages(r"C:\Working_Directory\R_" + str(i) + ".pdf") #appends the first PDF from the evens
  i = i + 1 #adds 1 to i so that it will append PDFs number 2 next, then 3, 4, and so on through 48finalPdf.appendPages(tempPDF)
# Update the properties of the final pdf
#
finalPdf.updateDocProperties(pdf_open_view="USE_THUMBS",
                             pdf_layout="SINGLE_PAGE")

# Save your result
#
finalPdf.saveAndClose()

# Delete variables
#
del finalPdf, mxdPathLeft, mxdPathRight, tempDDPLeft, tempDDPRight, 
tempMapLeft, tempMapRight, tempPDF


Here is the error...

Page 1 of 68

Traceback (most recent call last):
  File "C:\Working_Directory\PythonCombine - Copy.py", line 59, in <module>
    finalPdf.appendPages(r"C:\Working_Directory\R_" + str(i) + ".pdf") #appends the first PDF from the evens
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\_mapping.py", line 960, in appendPages
    return convertArcObjectToPythonObject(self._arc_object.appendPages(*gp_fixargs((pdf_path, password_for_pdf), True)))
AttributeError: Invalid path
>>>


I've checked the paths to the the .py file and the pages and the _mapping.py.  But I suspect their is something wrong with _mapping.py
__
Tags (2)
0 Kudos
10 Replies
StacyRendall1
Occasional Contributor III
The error makes it sound like a problem with the path you are passing to the function (although ArcGIS errors are seldom that informative).

You can put:
print r"C:\Working_Directory\L_" + str(i) + ".pdf"
print r"C:\Working_Directory\R_" + str(i) + ".pdf"

just above the line causing the error (59) and see that it prints exactly what you were expecting.

You could also try string formatting with double slashes, in case the regular expression bit is causing issues:
finalPdf.appendPages("C:\\Working_Directory\\L_%s.pdf" % str(i))


If neither of these helps track down the error, it may be an issue with Mapping...
0 Kudos
TomMagdaleno
Regular Contributor
Thanks for the tips Stacy.  I tried changing to string style and it gave me the exact same error.  I then tried adding the two lines above line 59 and I got an error at line 61. 

  finalPdf.appendPages(r"C:\\Working_Directory\\R_%s.pdf" % str(i)) #appends the first PDF from the evens


Which is interesting because it didn't fail on the first Left sheet, only when it tried to grab the first right sheet. 

Any ideas?
0 Kudos
StacyRendall1
Occasional Contributor III
Once you change from '\' in the path to '\\' you don't need the r at the start of the string. Did you possibly remove the r on line 60, but leave it at 61?
0 Kudos
curtvprice
MVP Esteemed Contributor

# Delete variables
#
del finalPdf, mxdPathLeft, mxdPathRight, tempDDPLeft, tempDDPRight, 
tempMapLeft, tempMapRight, tempPDF



Tom,

Just an aside...

I know you're being tidy, but all Python variables are deleted for you when the script ends. The only exception to this are ones that cannot be easily deleted because of other objects they are related to in memory, the two examples that come to mind are layers and table views that have active joins, and cursor objects.

As for your error, my guess is the pdf file you're trying to append (r"C:\Working_Directory\R_" + str(i) + ".pdf") does not exist. You can check for this with an if statement before you try to append it. ("if arcpy.Exists()" or "if os.path.exists()")

To keep my path stuff easier to manage, I almost always use os.path.join() instead of adding path strings together with +. Also, don't forget to use env.workspace to save yourself work constructing paths!

One more thing, the "%s" format code already converts your variable to a string representation, so you can just use:

r"C:\Working_Directory\R_%s.pdf" % i

or
r"C:\Working_Directory\R_{0}.pdf".format(i)
0 Kudos
KellyDoss2
New Contributor II

To anyone using this script, I only changed the file locations and was still getting the error messsage that Tom was getting above (I'm using version 10.2.2).  After hours of browsing websites/blogs for information that might help, I found that changing the few instances of "pgNum(Left/Right)" to "pageNum(Left/Right)" solved all my problems!

DarrenWiens2
MVP Honored Contributor

Can you elaborate on that? I don't see how simply changing a variable name could have solved anything, unless it was otherwise originally specified.

0 Kudos
KellyDoss2
New Contributor II

I'm not sure why it worked, as I'm a beginner at Python.  pgNum isn't a variable I defined.  Based on the error message I was getting (exactly the same as in the original post I replied to), and errors I could avoid by commenting out sections of my script, I assumed there was something off with the loops, so I started investigating.  I looked at various other similar scripts on blog sites and noticed that some of them were using "page" instead of "pg", so I decided to try it and it worked.  The small difference may just be based on the version of Python or Arc I'm using....

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

My guess is you are using the samples from this blog post Combining Data Driven Pages with Python and arcpy.mapping | ArcGIS Blog   

They show samples using both variables, but I'm wondering if pgNum is a reserved word.

0 Kudos
KellyDoss2
New Contributor II

Here is where I got the original script. http://resources.arcgis.com/en/help/main/10.2/index.html#//00s90000002p000000

0 Kudos