HOW TO

Export and rename attachments in a batch in ArcGIS Desktop

Last Published: September 15, 2020

Summary

Attachments from a feature class can be exported individually or in a batch to a desired local folder with default filenames. In some instances, the exported attachments must be renamed to match the values in a feature class field. This article provides the workflow to export and rename attachments from a feature class in a batch using a Python script in ArcGIS Desktop.

Procedure

Note:
When working in ArcMap, it is advisable to disable background geoprocessing. To do this, navigate to Geoprocessing > Geoprocessing Options, and uncheck the Enable check box under Background Geoprocessing.
  1. Add the feature class and its attachment table to ArcMap or the ArcGIS Pro project.
  2. Join the attachment table to the feature class, and set the feature class as the join target. Refer to Joining attributes in one table to another for steps in ArcMap, and Add Join (Data Management) for ArcGIS Pro.
  3. Run the Table To Table tool to make the join permanent, and set the location of the output table to a file geodatabase. Refer to Table To Table for more information on the tool in ArcMap, and Table To Table for ArcGIS Pro.
  4. Run a Python script to export and rename attachments from the output table.
    1. Open the Python window. Refer to Opening the Python window for steps in ArcMap, and Python window for steps in ArcGIS Pro.
    2. In the Python window, paste the following expression, and alter the parameters as necessary.
import arcpy
from arcpy import da
import os

#Modify the following to the desired location
#input_table must be the output table from Step 3
inTable = r"<input_table>"
fileLocation = r"<desired_folder>"

#Modify field_name1 and field_name2 to the desired field name
with da.SearchCursor(inTable, ['DATA', '<field_name1>', '<field_name2>']) 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
del cursor

The image below shows the Python script with the altered parameters. In this example, field_name1 and field_name2 are changed to TYPE and FileName respectively.

Image of the Python script.
  1. Press Enter to run the script. The attachments are exported to a folder with the customized name.
Image of the output attachments.

Article ID:000023581

Software:
  • ArcMap
  • ArcGIS Pro 2 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options