Select to view content in your preferred language

ExportToPDF script : allow overwrite validation

1206
4
Jump to solution
11-22-2012 04:48 AM
MaximeDemers
Regular Contributor
Hello,

I have a simple script that Export to Pdf the current Page_Layout of a mxd. I want to use it in a toolbox with a path parameter for the output. I have an error when I select an output that already exists. I suppose I have to code something about allowing overwriting in the validation of this parameter but I cant find any info about that.

Anyone can help?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Oh I see, I was under the impression your errors were showing up before you ran the tool. I assume in your script pdf is the variable of the path and name is the name of the pdf you want to create? Something like this should get you going.

import os import arcpy  mxd = arcpy.mapping.MapDocument("CURRENT") pdf = str(arcpy.GetParameterAsText(0)) name = str(arcpy.GetParameterAsText(1)) res = int(arcpy.GetParameterAsText(2)) quality = str(arcpy.GetParameterAsText(3)) path = os.path.join(pdf, name) if os.path.exists(path):     os.remove(path) arcpy.mapping.ExportToPDF(mxd, path, "Page_Layout", resolution=res, image_quality=quality)

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
You shouldn't be getting an error. It should just notify you that the file already exists and if you want to replace it. If you want you could just process it silently in the script by setting your parameter of your output PDF to a folder and specify the name in your script while handling if the pdf already exists.

Edit: You could also just specify your parameter as input instead of output. This will allow you to take the path as a string and validate on that in your code.
0 Kudos
MaximeDemers
Regular Contributor
Thank you for your answer,

Unfortunately, even with a Parameters with Data Type: Folder, I have the same error. It said that the folder already exist. Error 000725

When I specify the folder parameter as an input, i have an error saying that the file doesnt exist.
0 Kudos
MathewCoyle
Frequent Contributor
Oh I see, I was under the impression your errors were showing up before you ran the tool. I assume in your script pdf is the variable of the path and name is the name of the pdf you want to create? Something like this should get you going.

import os import arcpy  mxd = arcpy.mapping.MapDocument("CURRENT") pdf = str(arcpy.GetParameterAsText(0)) name = str(arcpy.GetParameterAsText(1)) res = int(arcpy.GetParameterAsText(2)) quality = str(arcpy.GetParameterAsText(3)) path = os.path.join(pdf, name) if os.path.exists(path):     os.remove(path) arcpy.mapping.ExportToPDF(mxd, path, "Page_Layout", resolution=res, image_quality=quality)
0 Kudos
MaximeDemers
Regular Contributor
Thank you very much! Thats perfect!
0 Kudos