Runtime error Traceback (most recent call last): File "<string>", line 17, in <module> WindowsError: [Error 5] Access is denied: 'R:\\GIS\\...\\PDF'
if multiPage == "true": if os.path.exists(outputPDF): os.remove(outputPDF) #Create new FinalMapBook that results will be appeneded into finalMapBook = arcpy.mapping.PDFDocumentCreate(outputPDF)
Solved! Go to Solution.
import arcpy, os, sys
#set map doc and the layer to be used
mxd = arcpy.mapping.MapDocument("Current")
mapLyr = arcpy.mapping.ListLayers(mxd, "Detail_2013")[0]
#Get input parameters
pageNumList = arcpy.GetParameterAsText(0).split(";")
multiPage = arcpy.GetParameterAsText(1)
outputPDF = arcpy.GetParameterAsText(2)
if multiPage == "true":
if os.path.exists(outputPDF):
os.remove(outputPDF)
#Create new FinalMapBook that results will be appeneded into
finalMapBook = arcpy.mapping.PDFDocumentCreate(outputPDF)
#Listing the text elements on the page
concatElem1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat1")[0]
concatElem2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "concat2")[0]
#Get page number from data driven page - specified in the tool parameter dialogue box
ddp = mxd.dataDrivenPages
for pageNum in pageNumList:
arcpy.AddMessage(pageNum)
pageID = mxd.dataDrivenPages.getPageIDFromName(str(pageNum))
mxd.dataDrivenPages.currentPageID = pageID
#Set layer definition query, this contols the rowcount variable
pageFieldValue = pageNum
mapLyr.definitionQuery = '"pageNum" = %s' % pageNum
#Finds the number of features in the map and sets up the lists to evenly distribute into the two text elements/columns
rowcount = int(arcpy.GetCount_management("Detail_2013").getOutput(0))
percolumn = round(rowcount / 2.0)
count1 = 1
count2 = rowcount
#Specifies the features being used for the SearchCursor
rows = arcpy.SearchCursor(mapLyr, "", "", "CONCAT")
fieldrow = arcpy.SearchCursor(mapLyr, "", "", "pageNum")
currentpage = ""
text_var1 = str()
text_var2 = str()
for row in fieldrow:
if currentpage != row.pageNum:
currentpage = row.pageNum
for row in rows:
if count1 <= percolumn:
text_var1 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
concatElem1.text = text_var1
count1 += 1
elif count2 > percolumn:
text_var2 += '{0}{1}'.format(row.getValue("CONCAT"), os.linesep)
concatElem2.text = text_var2
count2 - 1
else:
pass
else:
pass
#Generate multi-page PDF file or single PDF files
if multiPage == "true": #Appends all PDFs into single, multi-page PDF
arcpy.mapping.ExportToPDF(mxd, outputPDF[:-4] + pageNum + ".pdf") #strip away .pdf, add platnum value and add ".pdf"
finalMapBook.appendPages(outputPDF[:-4] + pageNum + ".pdf") #append page to final PDF
os.remove(outputPDF[:-4] + pageNum + ".pdf") #no longer needed once appended
else: #Create individual PDFs for each page
arcpy.mapping.ExportToPDF(mxd, outputPDF[:-4] + pageNum + ".pdf")
#Add PDF properties for multi-page PDF
if multiPage == "true":
finalMapBook.updateDocProperties("2010-2011 Crash Report", "2010-2011 Crash Report", "map sheets, map book", "USE_THUMBS")
finalMapBook.saveAndClose()
arcpy.RefreshActiveView()
arcpy.AddMessage("PROCESS COMPLETED")
#Removed the definition query so all page numbers appear when the script is run next and refresh he layout view
mapLyr.definitionQuery = ""
arcpy.RefreshActiveView()
del mxd, row, rows, rowcount, percolumn, count1, count2
scriptPath = os.path.dirname(sys.argv[0]) ccPath = os.path.join(os.path.dirname(scriptPath),"ToolData","NATOCountryCode.xml")
path = "c:\data\tools\script.py d:\work\output.pdf"
cleanedPath = path.split(" ")[1]When you say the tool won't run (after correcting the path issue), is it that you get the same error, or the tool dialog itself shows an issue (red X)?
If it's the tool dialog, then you might need to adjust how the parameter is set up. If it's set up as a 'file', validation does check if the file exists.
In the dialog box I get a red "X", why does the file need to exist beforehand? Okay, I exported a page so I had a pdf file to replace and it worked just fine. Does this seem odd? And I have to browse to the folder and select the file. For some reason, even though the path and file a specified, it won't go unless I manually choose that file.