<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Automate adding Filepath to attributes in table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1189353#M64907</link>
    <description>&lt;P&gt;Thank you for the help! I had to make some edits to make this work with my data, but this was pretty much exactly what I was looking for!&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 05 Jul 2022 12:35:53 GMT</pubDate>
    <dc:creator>Caitlin</dc:creator>
    <dc:date>2022-07-05T12:35:53Z</dc:date>
    <item>
      <title>Automate adding Filepath to attributes in table</title>
      <link>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1187813#M64857</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I'm working to create a python script in Arcpro notebooks to add the filepath of a PDF document associated with each attribute in a table to a new field in that same table.&lt;/P&gt;&lt;P&gt;The issue is that the PDF documents are in different folders (layout shown below):&lt;/P&gt;&lt;P&gt;MAINFOLDER&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; A&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ADDRESS1.pdf&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ADDRESS2.pdf&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME2&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME3&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; B&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME1&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ADDRESS1.pdf&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ADDRESS2.pdf&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME2&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; STREETNAME3&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; C&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If all of the PDF documents had the same path, I would just use an updatecursor to add in the path + ADDRESSField. Since the path is different, I'm not sure how to add the correct path to each attribute.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, I've used the glob module to add all of the PDF filepaths to a list, and I have a list of lists that contains the attribute fields for both STREET_NUM and STREETNAME that I'm using to associate each attribute with the correct table.&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;P&gt;import glob&lt;BR /&gt;import os&lt;/P&gt;&lt;P&gt;os.chdir('M:\Service Sheets')&lt;/P&gt;&lt;P&gt;servicePath = glob.glob("*/*/*.pdf") # Here is the list of all PDF files in the directory&lt;BR /&gt;fc = "Parcel_Subset"&lt;BR /&gt;fields = ["STREET_NUM", "STREETNAME", "PDFLink"] # PDFLink is where the output will go&lt;BR /&gt;LocList = []&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;with arcpy.da.SearchCursor(fc, ['STREET_NUM', 'STREETNAME']) as cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; for row in cursor:&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; LocList.append(row) # Creates the List of Lists&lt;/P&gt;&lt;P&gt;# Here would be the updatecursor, if I could get it to work&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure this is the best method, or if there's an easier way that I just don't know. This would also just add a text field with the path, if possible, it would be better to make the path a clickable link that opened the PDF document.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice or solutions would be appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:46:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1187813#M64857</guid>
      <dc:creator>Caitlin</dc:creator>
      <dc:date>2022-06-29T14:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Automate adding Filepath to attributes in table</title>
      <link>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1187940#M64863</link>
      <description>&lt;P&gt;In regards to the second part of the question, you can format the output to be in HTML, which will be read as a link.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;a href="C:\Users\USER\...\Lorem Ipsum.docx" target=_top"&amp;gt;Lorem Ipsum.docx&amp;lt;/a&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 18:00:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1187940#M64863</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2022-06-29T18:00:46Z</dc:date>
    </item>
    <item>
      <title>Re: Automate adding Filepath to attributes in table</title>
      <link>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1188697#M64883</link>
      <description>&lt;P&gt;I'm not sure how you are tying the street number and streetname to the file names in the folders, but as an idea to your first part of the question, you could use a dictionary to store all of the files and their paths. I can't really tell what attributes ties to what folder/ file, so this is only an idea that you can expand on if it looks doable with your data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def recursive_walk(parentFolder):
    pdfDict = {}

    for folderName, subfolders, filenames in os.walk(parentFolder):
        if subfolders:
            for subfolder in subfolders:
                recursive_walk(subfolder)

        for filename in filenames:
            print(os.path.join(folderName, filename))
            pths = folderName.split(os.sep)
            pdfDict[pths[-2]+pths[-1]+filename.replace('.pdf', '')] = { 'attrMatch': pths[-1] +filename.replace('.pdf', ''),  filename.replace('.pdf', '') : os.path.join(folderName, filename) }


    return pdfDict

pdfDict = recursive_walk(r'C:\Users\Documents\PDFs')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;provides a dictionary like:&lt;/P&gt;&lt;P&gt;{'AStreetname1address1': {'attrMatch': 'Streetname1address1', 'address1': 'C:\\Users\\Documents\\PDFs\\A\\Streetname1\\address1.pdf'},&lt;BR /&gt;'AStreetname1address2': {'attrMatch': 'Streetname1address2', 'address2': 'C:\\Users\\Documents\\PDFs\\A\\Streetname1\\address2.pdf'},&lt;BR /&gt;'AStreetname2address1': {'attrMatch': 'Streetname2address1', 'address1': 'C:\\Users\\Documents\\PDFs\\A\\Streetname2\\address1.pdf'},&lt;BR /&gt;...&lt;BR /&gt;'CStreetname3address1': {'attrMatch': 'Streetname3address1', 'address1': 'C:\\Users\\Documents\\PDFs\\C\\Streetname3\\address1.pdf'},&lt;BR /&gt;'CStreetname3address2': {'attrMatch': 'Streetname3address2', 'address2': 'C:\\Users\\Documents\\PDFs\\C\\Streetname3\\address2.pdf'}}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then in the cursor, get the path by looking up the combo of attributes:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(fc, ['STREET_NUM', 'STREETNAME', 'PDFLink']) as cursor:
    for row in cursor:
        attrConcat = f"{row[1]}{row[0]}" # create the unique combination to match the key format in the dictionary
        for k, v, in pdfDict.items(): # test if the key combo has a dictionary entry
            if v['attrMatch'] == attrConcat: # get the values from the matching key
                attr, path = v.items()
                row[2] = fr'''&amp;lt;a href="{path[1]} target=_top"&amp;gt;{attr[1]}&amp;lt;/a&amp;gt;''' # set the PDFLink to the path from the dict
                cursor.UpdateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 01 Jul 2022 13:43:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1188697#M64883</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-07-01T13:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Automate adding Filepath to attributes in table</title>
      <link>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1189353#M64907</link>
      <description>&lt;P&gt;Thank you for the help! I had to make some edits to make this work with my data, but this was pretty much exactly what I was looking for!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 12:35:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/automate-adding-filepath-to-attributes-in-table/m-p/1189353#M64907</guid>
      <dc:creator>Caitlin</dc:creator>
      <dc:date>2022-07-05T12:35:53Z</dc:date>
    </item>
  </channel>
</rss>

