2

I am trying to convert regular python script to toolbox. This is the regular code which works:

import arcpy
strOutpath = "C:\Users\name\Desktop\New Folder"
mxd = arcpy.mapping.MapDocument("CURRENT")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
 mxd.dataDrivenPages.currentPageID = pageNum
 pageorder = mxd.dataDrivenPages.pageRow.BR
 pageindex = mxd.dataDrivenPages.pageRow.KARTA
 pdfname = pageorder + "-" + pageindex + ".pdf"      
 arcpy.mapping.ExportToPDF(mxd, strOutpath + pdfname, resolution="300dpi", image_compression="Adaptive", jpeg_compression_quality="85", colorspace="RGB", picture_symbol="VECTORIZE_BITMAP")
del mxd

And this is the my example for script to input as script for toolbox:

import arcpy
strOutpath = arcpy.GetParameterAsText(0)
mxd = arcpy.GetParameterAsText(1)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
 mxd.dataDrivenPages.currentPageID = pageNum
 pageorder = arcpy.GetParameterAsText(2)
 pageindex = arcpy.GetParameterAsText(3)
 pdfname = pageorder + "-" + pageindex + ".pdf"
 arcpy.mapping.ExportToPDF(mxd, strOutpath + pdfname, resolution="300dpi", image_compression="Adaptive", jpeg_compression_quality="85", colorspace="RGB", picture_symbol="VECTORIZE_BITMAP")

As parameters, I entered this:

strOutpath = FOLDER 
mxd = ArcMap document 
pageorder = field 
pageindex = field

And when I try to input the values, I get empty fields. There is clearly something wrong with the second code and input variables, but I don't know what.

empty field for input

EDIT:

When I edit my code, based on the comment ArtWork21 gave, when only output folder and mxd are user-generated, this is the code:

import arcpy
strOutpath = arcpy.GetParameterAsText(0)
mxd = arcpy.GetParameterAsText(1)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
 mxd.dataDrivenPages.currentPageID = pageNum
 pageorder = mxd.dataDrivenPages.pageRow.BR
 pageindex = mxd.dataDrivenPages.pageRow.KARTA
 pdfname = pageorder + "-" + pageindex + ".pdf"
 arcpy.mapping.ExportToPDF(mxd, strOutpath + '\\' + pdfname, resolution="300dpi", image_compression="Adaptive", jpeg_compression_quality="85", colorspace="RGB", picture_symbol="VECTORIZE_BITMAP")
del mxd

This is the error:

Traceback (most recent call last):
File "C:\Users\name\Desktop\script.py", line 4, in <module>
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
AttributeError: 'unicode' object has no attribute 'dataDrivenPages'
Failed to execute (Script).

And these are my variables:

enter image description here So, now I now I am just trying zo get the last one script to work, where I only enter output folder, and input mxd.

  • What do you mean by "when I try to input the values, I get empty fields"? Also, on your last export statement you need to add slashes to the output file name path like strOutpath + '\\' + pdfname – artwork21 Jun 23 '17 at 12:32
  • edited the question – Dean7 Jun 23 '17 at 12:45
  • 1
    You cannot dynamically get the page order and index from a script tool parameter. I would leave those two statements how you had them in the first example. – artwork21 Jun 23 '17 at 12:57
  • Didn't know that. I will try to edit my code now, with your input. – Dean7 Jun 23 '17 at 13:40
3

You need to still make the map document object from its parameter path, and make sure the data driven page is enabled and saved within the map doc, try:

mxdPath = arcpy.GetParameterAsText(1)
mxd = arcpy.mapping.MapDocument(mxdPath)

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  # continue on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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