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
1 Answer 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.
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– vss0317122017年01月12日 17:07:33 +00:00Commented Jan 12, 2017 at 17:07
-
@vss031712 To show your appreciation I think you should Accept and Upvote the answer.2017年01月16日 10:39:14 +00:00Commented Jan 16, 2017 at 10:39
-
I'm not sure how to go about doing that.vss031712– vss0317122017年01月17日 14:50:39 +00:00Commented Jan 17, 2017 at 14:50
Explore related questions
See similar questions with these tags.
arcpy.AddMessage(in_folder + file_name)
after thefile_name
variable, and then check the Geoprocessing Results window to see the message.