[GIS] Python Field Calculating for Document Hyperlink

arcpyfield-calculatorhyperlink

I am trying to create a script that will hyperlink to a .tiff or .pdf. These files are stored in one folder on a shared server.

I want the script to look in a field: 'PLAT_IMAGE', which has the plat number (ex: 12048-001-01) and find it's match in the folder on the shared server and then write for example:

\\server\engineering\Asbuilts\Plats\12048-001-01.tif

Making the document hyperlink active in ArcMap when using Identify

My problem is that the calculation is going through and not giving me any errors but yet it's not calculating the field, so I don't know what I'm doing wrong!

import os, arcpy, fnmatch

def  x(PLAT_IMAGE):
    path = "J:\\engineering\\Asbuilts\\Plats"
    plat = PLAT_IMAGE
for file in os.listdir(path):
    if os.path.isdir(os.path.join(path, file)):
        for file in os.listdir(os.path.join(path, file)):
            if fnmatch.fnmatch(file,plat + '.*'):
                return "\\" + "\\mercury\gisdata$\engineering\Asbuilts\Plats" + "\\" + file

x( !PLAT_IMAGE! )

Best Answer

Danielle,

Try this code below... note that if you prefix a string with r (like I did in the code), you don't need to "escape the backslashes"...

With this you can specify which extensions to look for, and in what order... so for instance if you have a .pdf and a .tif of the same file, it will use the .pdf because .pdf was listed first in the fileNames list.

If the file doesn't exist, then it just returns a blank hyperlink...

import os

def GetHyperlink(PLAT_IMAGE):
    folderPath = r"J:\engineering\abuilts\path" + "\\"
    fileNames = []
    fileNames.append(PLAT_IMAGE + '.pdf')
    fileNames.append(PLAT_IMAGE + '.tif')
    fileNames.append(PLAT_IMAGE + '.tiff')

    hyperlink = ''

    for fileName in fileNames:
        if os.path.isfile(folderPath + fileName):
            hyperlink = r"\\mercury\gisdata$\engineering\Asbuilts\Plats" + "\\" + fileName
            break;

    return hyperlink