English

How To: Batch export attachments from a feature class in ArcGIS Pro

Summary

ArcGIS Pro provides the capability to export attachments from a feature class by saving the attached file to a file system, as described in ArcGIS Pro: Save an attached file. However, this capability is limited to saving the attachments of only one feature at a time. There is no geoprocessing tool for users to export or save attachments to all features in one process.

The instructions provided describe a method to batch export attachments from a feature class by creating a script tool.

Note:
The workflow requires a Desktop Standard or Desktop Advanced license.

Procedure

To batch export attachments from a feature class in ArcGIS Pro, create a script tool from a Python script that enables exporting attachments in batches from a feature class. Run the script tool by providing the parameters required.

Note:
The script provided requires the standard attachment table, which is created when attachments are enabled as the input on a feature class. This is because it relies on the DATA, ATT_NAME and ATTACHMENTID fields stored in the table. A typical naming convention is to append _ATTACH to the end of the table name.
  1. Copy and paste the following script into a Notepad, and save it as ExportAttachments.py.
Note:
The following script iterates through the entire attachment table of a single feature class and copies the attachments (saved as BLOBs or binary large objects) to a file.
import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)

with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
    for item in cursor:
        attachment = item[0]
        filenum = "ATT" + str(item[2]) + "_"
        filename = filenum + str(item[1])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment
  1. In ArcGIS Pro, right-click a toolbox in the Catalog pane, and click New > Script
An image of the steps to create a new script tool in a toolbox
  1. On the script tool property page, set the properties of the script tool as follows:
    1. In the General section, type the Name and Label for the script tool. For example, type ExportAttachments as the Name, and Export Attachments as the Label. The label displays the name of the script tool, as shown in the Geoprocessing pane.
    2. Click the Browse icon An image of the Browse icon next to the Script File field, and navigate to the location of the script file saved in Step 1. Select the script and click OK.
    3. Check the Store tool with relative path check box.
An image of the General section of the script tool property page.
  1. In the Parameters section, type Attachments Table in the first row of the Label column, and set the Data Type as Table. This is the first parameter of the script tool and it defines the table containing the attachments to export.
  2. In the second row, add a second parameter with the Label name as Output Location, and set the Data Type as Folder. This parameter defines the folder to locate the exported attachments.
An image of the Parameters section of the script tool property page.
  1. Click OK. A new script tool is added to the toolbox.
An image of the created Export Attachments script tool.
  1. Double-click to open the created script tool. Set the parameters of the tool as follows to export the attachments in a batch.
    1. In the tool dialog box, browse for the attachments table containing the attachments to export in the Attachments Table parameter.
    2. In the Output Location parameter, select a folder to save the exported attachments, and click OK.
    3. Click Run.
An image of the Export Attachments tool dialog box.
The attachments are successfully exported in a batch, and located in the folder defined in the Output Location parameter.
An image of the list of the exported attachments

Related Information