I use the following code to do just that.
In this case, all my attachments are .jpg files, so I just append that filename extension. If you have other/mixed file types, would need to modify to extract that info from the _ATTACH table.
import arcpy
import os
inFeatureClass = r'Database Connections\...\Signs' # feature class with attachments
pointFieldsList = ['GlobalID','MUTCDcode'] # fields to load into cursor
global_dict = {} # dictionary to hold globalIDs and "name" field
fileLocation = r'C:\tmp\Attachments' # folder where attachments are saved
with arcpy.da.SearchCursor(inFeatureClass, pointFieldsList) as point_cursor: # cursor to make dict of globalID's
for row in point_cursor:
if row[1]:
row1 = row[1] # I'm using "MUTCDcode" as my name field, but is
else: # not always populated. if Null, substitute "NA"
row1 = "NA"
global_dict[row[0]] = [row[0],row1] # add to dict with globallID as key, name as value
inTable = r'Database Connections\SignDatabase.sde\SignsDatabase.DBO.Sign__ATTACH' # attachment table for feature class
tableFieldsList = ['REL_GLOBALID', 'DATA', 'ATT_NAME', 'ATTACHMENTID'] # field list in attachment table
with arcpy.da.SearchCursor(inTable, tableFieldsList) as table_cursor:
for item in table_cursor:
attachment = item[1] # grab attachemnt from blob field
filename = global_dict[item[0]][1] + '_' + str(item[3]) + '.jpg' # create filename from value in FC dict that has matching GlobalID
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes()) # write the attachment as a file.
R_