Create Excel Hyperlinks in Python
<p>I am using win32com to modify an Excel spreadsheet (Both read and edit at the same time) I know there are other modules out there that can do one or the other but for the application I am doing I need it read and processed at the same time.</p>

<p>The final step is to create some hyperlinks off of a path name. Here is an Example of what I have so far:</p>

<pre><code>import win32com.client


excel = r'I:\Custom_Scripts\Personal\Hyperlinks\HyperlinkTest.xlsx'

xlApp = win32com.client.Dispatch("Excel.Application")
workbook = xlApp.Workbooks.Open(excel)
worksheet = workbook.Worksheets("Sheet1")


for xlRow in xrange(1, 10, 1):
    a = worksheet.Range("A%s"%(xlRow)).Value
    if a == None:
        break
    print a

workbook.Close()
</code></pre>

<p>I found some code for reading Hyperlinks using win32com:</p>

<pre><code>sheet.Range("A8").Hyperlinks.Item(1).Address
</code></pre>

<p>but not how to set hyperlinks</p>

<p>Can someone assist me?</p>

<p>Borrowing heavily from <a href="https://www.experts-exchange.com/questions/21349668/Using-win32com-with-Python.html" rel="nofollow">this</a> question, as I couldn't find anything on SO to link to as a duplicate...</p>

<p>This code will create a Hyperlink in cells <code>A1:A9</code></p>

<pre><code>import win32com.client

excel = r'I:\Custom_Scripts\Personal\Hyperlinks\HyperlinkTest.xlsx'

xlApp = win32com.client.Dispatch("Excel.Application")
workbook = xlApp.Workbooks.Open(excel)
worksheet = workbook.Worksheets("Sheet1") 

for xlRow in xrange(1, 10, 1):
    worksheet.Hyperlinks.Add(Anchor = worksheet.Range('A{}'.format(xlRow)),
                             Address="http://www.microsoft.com",
                             ScreenTip="Microsoft Web Site",
                             TextToDisplay="Microsoft")
workbook.Save()
workbook.Close()
</code></pre>

<p>And here is a link to the <a href="https://msdn.microsoft.com/en-us/library/office/ff822490.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow">Microsoft Documentation</a> for  the <code>Hyperlinks.Add()</code> method.</p>

python excel python-2.7 win32com