0

I'm very new to writing my own scripts.

What I am trying to accomplish is exporting JPEGs with a world file that follow data driven pages. I would like to automate this process as much as possible, creating a tool for it.

The following script I was able to get to work if I am in the mxd file and create a folder previously that I add to the script (this test run I used C:\Data\test\PIC), then run it . Here it is:

    import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
...     mxd.dataDrivenPages.currentPageID = pageNum
...     print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
...     arcpy.mapping.ExportToJPEG(mxd, r"C:\Data\test\PIC" + str(pageNum) + ".jpeg",df,df_export_width=1600,df_export_height=1200,world_file=True)
del mxd
del df

Now I'd like to take it a step further, creating a tool. Something with parameters that allow me to pick the folder, the file name,, the width, height, etc. So I did a test with an input for folder and file name. It didn't work, all it did was open up the script in Notepad when it was complete.

What am I doing wrong?

Am I going about this in the right way at least?

The parameters I added were Folder for 0 and String for 1. Here's the new script I tried when I made it into a tool:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

in_folder = arcpy.GetParameterAsText(0)
file_name = arcpy.GetParameterAsText(1)


for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
...     mxd.dataDrivenPages.currentPageID = pageNum
...     print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
...     arcpy.mapping.ExportToJPEG(mxd, in_folder + file_name + str(pageNum) + ".jpeg",df,df_export_width=1600,df_export_height=1200,world_file=True)
del mxd
del df
  • Have you configured the tool dialog, and tried running it from there? – PolyGeo Dec 15 '16 at 21:59
  • 1
    What errors did you get? It could be a problem with your file path. Try adding arcpy.AddMessage(in_folder + file_name) after the file_name variable, and then check the Geoprocessing Results window to see the message. – ianbroad Dec 16 '16 at 5:11
  • It doesn't seem to pop up any error when I run it. When I run it, it opens a blank MS-DOS window and the script in Notepad. I X out of the script and the dialog box says its completed, with no errors. – vss031712 Dec 16 '16 at 15:15
  • As far as configuring the tool, not sure how to go about doing that. I created it in ArcCatalog, right clicked on my toolbox and clicked on add script. Gave it a description, added the script file, added the parameters I thought were fitting, and clicked okay. Is this the process I go through? – vss031712 Dec 16 '16 at 15:18
1

You need to change

in_folder + file_name

to

in_folder + "\\" + file_name

In your ExportToJPEG

I got your code to work just fine after I made it into a tool.

enter image description here

Also remove the ... from your code, here's what worked for the tool.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

in_folder = arcpy.GetParameterAsText(0)
file_name = arcpy.GetParameterAsText(1)

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    arcpy.AddMessage( "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount)))
    arcpy.mapping.ExportToJPEG(mxd, in_folder + "\\" + file_name + str(pageNum) + ".jpeg",df,df_export_width=1600,df_export_height=1200,world_file=True)
del mxd
del df
  • Awesome. I got it working after messing around with it some more, but I'm going to try your way and see what it does. Thanks for the response. – vss031712 Jan 12 '17 at 17:07
  • @vss031712 To show your appreciation I think you should Accept and Upvote the answer. – PolyGeo Jan 16 '17 at 10:39
  • I'm not sure how to go about doing that. – vss031712 Jan 17 '17 at 14:50

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.