Useful Scripts for the Non-Programmer: Converting VBA Scripts to Python

February 4, 2014 — Corey Blakeborough

Update (September 2014): We’ve now added a 10.2 version of Snippets.py to the article.

Update (February 2014): These instructions have been updated to be compatible with the ArcGIS 10.1 release. Check them out for some added versatility for your 10.1 setup. We will be looking into adding 10.2 compatibility in the future as well.

 


 

(Originally published in March, 2012 Energy Advisor)

Throughout the months that we’ve sent out Energy Advisor articles, we have provided you with a numerous amount of useful scripts that serve various purposes, most of these scripts being written in VBA (Visual Basic for Applications). Both Microsoft and Esri have made strides to push users away from the continued use of Visual Basic, and as such, VBA support is not included by default in ArcGIS 10 (albeit still supported at the “Mature” level by Esri).

The Visual Basic Editor in both ArcMap and ArcCatalog has certainly proven useful over the years, but as we say goodbye, we want to ensure that we can transition smoothly over to an alternative.

Python certainly does have advantages. It’s included with ArcGIS installations, it’s faster for COM Interop than VBA, and it uses ArcPy to offer extended functionality including access to programmatically run ArcToolbox tools. That said, there is a lot of functionality still available only through ArcObjects, and you probably used ArcObjects fairly extensively in VBA scripts (we certainly did!). With that in mind, our goal is to get ArcObjects working with Python. That part isn’t as easy, but this article does its best to walk you through a commonly-used method.

A thorough amount of Python documentation is available online.

Allowing Python to Use ArcObjects

Our first hurdle is allowing Python to manipulate COM objects. The most common route here is to download comtypes from SourceForge. Run the installer and choose to attach it to the version of Python included with ArcGIS. Normally there should only be one.

Note for versions 10.1 and above: When working to get ArcObjects and Python working together in version 10.1+, you’ll also need to make some modifications to your comtypes installation. The full solution is abbreviated here:

  1. Navigate to your comtypes directory
    (ex. C:\Python\ArcGIS10.X\Lib\site-packages\comtypes)
  2. Delete  the following  files (note that the “safearray” files are not preceded by an underscore):
    automation.pyc
    automation.pyo
    safearray.pyc
    safearray.pyo
  3. Open automation.py with a text editor and go to line 794 which is part of the “ctype_to_vartype” dictionary. Add this line (including the comma):
    POINTER(BSTR): VT_BYREF|VT_BSTR,
  4. Delete everything from the “gen” directory.

Technically, that’s all you need, but there will still be quite a labor involved to use ArcObjects. With that in mind, many developers – myself included – have been using a “Snippets” module developed by Mark Cederholm. This module was initially written for ArcGIS 9.x, and we have updated them to work for future ArcGIS releases. You can download specific versions of Snippets.py below.

ArcGIS Version
9.X
10.0.X
10.1.X
10.2.X

 

Move your newly-downloaded Snippets.py to your appropriate ArcGIS Python folder – most likely something along these lines: C:\Python[version]\ArcGIS[version]\Lib

Now we can import both comtypes and Snippets into our Python scripts, making ArcObjects easy – arguably much easier than it even was for VBA (at least after the setup)!

    import Snippets
import comtypes

From here, you can manually import ArcGIS libraries. Here’s an example of importing ESRIFramework:

Snippets.GetModule(“esriFramework.olb”)

After that, Framework would be available through comtypes.gen.esriFramework, or you could run the following line to access all of esriFramework without using that prefix:

from comtypes.gen.esriFramework import *

Wondering how the filename “esriFramework.olb” was generated? The function Snippets.GetLibPath() shows the directory where the COM libraries are located. You can find the name inside of that folder. Import snippets and run GetLibPath() within the Python window (explained below) to get the information quickly.

Additionally, Snippets provides functions that retrieve commonly-used Esri libraries in bulk – Snippets.GetDesktopModules() opens esriFramework and esriArcMapUI, while Snippets.GetStandaloneModules() opens esriSystem, esriGeometry, esriCarto, esriDisplay, esriGeoDatabase, esriDataSourcesGDB, esriDataSourcesFile, and esriOutput. You could also edit Snippets.py to make similar module-grabbing methods or edit existing ones to grab everything you need.

After you go through this process, ArcObjects will be available to you to freely use with Python.

If you need to use ArcFM™ objects in your script, we can help with that, too.

Viewing the Python Window

Esri provides a runtime Python execution method called the Python window. By default, it is available on ArcCatalog’s “Standard” toolbar, and not visible in ArcMap, but it can be added via the Customize window.

useful-scripts-python-window-button

useful-scripts-python-window

The Python window allows you to execute any Python code in real-time. You will still need to run any import statements, etc. as usual. As a tip, to retrieve the current application, use Snippets.GetApp() for ArcMap, or Snippets.GetApp(‘ArcCatalog’) for ArcCatalog.

The right side of the window shows tips and function parameters when applicable. Remember that you can use Shift + Enter to move to a new line without executing the code. This will allow you to perform loops, if statements, and much more.

useful-scripts-python-window-tips.

You can even load and save scripts by right-clicking the window and choosing “Load” or “Save As”, though I’d recommend using a standalone editor to make your scripts.

Creating a Python Script

ArcGIS provides a Python GUI called “IDLE”, which you may use for creating and saving your scripts. Alternatively, simply create a file ending in .py with any text editor. You will reference the file later for use in ArcMap or ArcCatalog, so save it in a good location.

useful-scripts-python-script

With the proper knowledge, scripts can be converted to Python relatively easily from this point onward using Snippets and comtypes (as explained earlier).

Adding Python Scripts to a Toolbox

If you recall, I mentioned that Python can access ArcToolbox and the built-in tools therein. ArcToolbox is also one of the most convenient ways to locally store your scripts. For consistency, we’ll use the ArcCatalog tree in both ArcMap and ArcCatalog. The Catalog Tree is located on the left side of ArcCatalog by default, and can be accessed in ArcMap by clicking WindowsCatalog.

useful-scripts-catalog-tree

Under “Toolboxes”, right-click “My Toolboxes” and click New→Toolbox. This saves a toolbox locally on the machine you’re using, in your user’s Application Data folder. Name it something appropriate – either a catchall for all your custom scripts, or make different toolboxes based on the common purposes of certain scripts.
useful-scripts-custom-toolbox

Right-click your new toolbox and click AddScript. When filling out the details, note that the “Name” field can’t have any spaces or underscores, and the “Label” field indicates what you’ll see the script called within ArcToolbox. It also may be beneficial to check the “Store relative path names (instead of absolute paths)” checkbox. Click “Next”.
useful-scripts-add-script

Navigate to your script, check the box to show the command window during the script execution, and click “Next”.

If you’re adding a script from another source, you should assume that no parameters are needed unless specified. If you do need to add them, simply enter the parameter name on the left side of the grid, and the description on the right. Extra properties, like default values, are in the lower table.

useful-scripts-add-script-parameter-properties

NOTE: If you do want to use parameters in your code, just import arcpy and use the arcpy.GetParameterAsText() function to get specific parameters.

You can now run your scripts by double clicking them in your toolbox, and running them from there.

Example

All of our scripts will eventually be converted to Python so that they can be used up to and beyond the point of no return for VBA (don’t worry, we’ll keep the VBA code as well). The above screenshots reference a rewritten copy of the script we provided in VBA that exports attributes of selected features. That script is now available for download in its new Python flavor here. To test out these methods, please feel free to download the script and add it to a new toolbox as described in the previous sections, then attempt to run it on features you’ve selected. As a bonus, this script uses parameters, so be sure to add them in:

Name Data Type Default Value
@FieldName String
@FileName String C:\Temp\featureAttributeExport.txt

We Wrote the Book

The Indispensible Guide to ArcGIS Online

Download It for Free

4 comments

What do you think?

Leave a comment, and share your thoughts

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.