How To: Batch export attachments from a feature class in ArcGIS Pro
ArcGIS Pro provides the capability to export attachments from a feature class by saving the attached file to a file system, as described in the following document: 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 that allows users to export or save all attachments that are attached 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 to accomplish the task.
To batch export attachments from a feature class in ArcGIS Pro, create a script tool from a Python script that allows exporting attachments in a batch from a feature class, using the workflow provided below, and run the created script tool by providing the parameters that are required by the tool.
Note: The script used to export the attachments requires the input table to be the standard attachment table created when attachments are enabled on a feature class. This is because the script relies on the DATA, ATT_NAME and ATTACHMENTID fields stored in this table. The typical naming convention of the attachment table must append _ATTACH to the end of the table name.
- Copy and paste the following script into Notepad, and save it as ExportAttachments.py.
Note: The following script iterates through the entire attachment table of a single feature class and copies all 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 filenum = "ATT" + str(item) + "_" filename = filenum + str(item) open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes()) del item del filenum del filename del attachment
- In ArcGIS Pro, right-click a toolbox in the Catalog pane, and click New > Script
- In the script tool property page, set the properties of the script tool as below.
- 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 is the display name for the script tool, as shown in the Geoprocessing pane.
- Click 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.
- Check the Store tool with relative path check box.
- 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. This parameter defines the table containing the attachments to export.
- 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.
- Click OK. A new script tool is added to the toolbox.
- Double-click the created script tool to open the tool. Set the parameters of the tool as below to export attachments in batch.
- In the tool dialog, browse for the attachments table containing the attachments to be exported in the Attachments Table parameter.
- In the Output Location parameter, select a folder to save the exported attachments, and click OK.
- Click Run.
- How To: Batch export attachments from a feature class
- ArcGIS Pro Help: Create a script tool
- ArcGIS Pro Help: Adding a script tool
- ArcGIS Pro Help: Setting script tool parameters