在Python中创建Excel超链接

问题描述:

我正在使用win32com修改Excel电子表格(同时读取和编辑)我知道那里还有其他模块可以执行一个或另一个,但是对于我正在执行的应用程序,我需要读取并处理它同时.

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.

最后一步是根据路径名创建一些超链接.这是我到目前为止的例子:

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

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()

我找到了一些使用win32com读取超链接的代码:

I found some code for reading Hyperlinks using win32com:

sheet.Range("A8").Hyperlinks.Item(1).Address

但不设置超链接

有人可以帮助我吗?

Borrowing heavily from this question, as I couldn't find anything on SO to link to as a duplicate...

此代码将在单元格A1:A9

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()

这是

And here is a link to the Microsoft Documentation for the Hyperlinks.Add() method.