Getting started with Python on ArcGIS is easier than you can think. It is not required to master Python to perform a basic use in our work since ArcGIS and ESRI offer a complete and plenty of code examples. Reading the following Mapping Gis article ( http://mappinggis.com/2015/10/exportar-un-mxd-a-pdf-con-python/ ) has largely inspired what follows.
Therefore we will discuss how to generate a pdf file based on the map of an ArcGis project (.mxd),without opening the project . Let’s suppose this document has to be updated according to frequent updates of the data.
We invite you to read the following article ArcGis and Python: before taking the first steps to see in more detail the different options to run our process with a Python code.
Here we will discuss three options: with the command window, with the ArcGis Python console and with the Python console of another IDE.
Let’s proceed in order. First we must find the Python code that interests us.
Searching the Python code with ArcGIS help
Go to the address http://desktop.arcgis.com/fr/
In the search field type the keywords: mxd to pdf arcpy
In the search result you find ExportToPDF -Help | ArcGIS for Desktop
By clicking this link you will find the help page for the code we are interested in : http://desktop.arcgis.com/fr/desktop/latest/analyze/arcpy-mapping/exporttopdf.htm
You find, in particular , two fundamental subsections :
The Python syntax of the command
And code examples
For this example you can immediately see that all that’s needed to execute the command is the name of the mxd and the name of the pdf file to produce.
Execution using the command line
Let’s use as example a simple map with the campsites open to the public as of today. The layer campsite is kept up to date by another process. What we want is to produce the pdf of the present day. Here is the layout window of the mxd document:
To execute the command, we open a command prompt window:
You launch Python by simply typing python
the three angle brackets (>>>) indicate that you can enter your Python command .
The first order is to load in Python the ArcGis functions. Type
import arcpy
This is all you have to do to be able to run our command that will generate our pdf file from the mxd. Afterwards, we enter the order :
mxd = arcpy.mapping.MapDocument (r C: \ Quimper \ geocoding.mxd “)
arcpy.mapping.ExportToPDF ( mxd, r “C: \ Quimper \ Campings.pdf “)
del mxd
(you can copy- paste directly the three lines into the window Command Prompt)
and we get immediately the pdf file
Note that the date being ArcMap text dynamic is filled in with actual date.
If you store the order lines in a text file, with an extension. py, you can run the entire order in a single command line:
Open the window Command Prompt, and then type
python.exe campings.py
You get the same result in pdf.
Execution using the ArcGis Python console
The advantage of executing the Python code from the ArcMap Python console lies mainly in the benefit from the syntactical aid.
As you can perceive by consulting the syntax of our command, there is a whole set of optional parameters. For example, the definition of the pdf output. By default, it is 300dpi. If you want it to be 600 dpi you must add the option in the command line
arcpy.mapping.ExportToPDF ( mxd, r “C: \ Quimper \ Campings.pdf “Resolution = 600)
In this case it is easier to use the Python console:
We open the Python console by clicking in the ArcMap window the corresponding button
You do not have to enter the arcpy import line in the ArcMap console.
When you type the command arcpy.mapping.ExportToPDF , you will see the syntax of this one appear in the right window :
Once executed , the command produces the same pdf file than before .
From the console of another IDE
The different integrated development environments are, in theory, intended to be executables. But, obviously, they hold interpretation consoles equivalent to the ArcMap Python console.
For example, PyScrpter, allows you to enter and run both control lines. The difference lies on the writing help for the commands:
Generally you will have more features offered by an IDE than through the ArcMap Python console. But if you keep on using the command line, both are equivalent. The main difference is that IDE will allow you to write, test and debug the whole Python scripts.