Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a hyperlink URL from an Excel document

Tags:

python

xlrd

I am reading an Excel file using xlrd. In one column I have a company name which is formatted as a hyperlink (meaning there is a URL behind it). When I get the cell value I only get the company name. How can I also get the URL behind it?

Below is the code for reading an Excel file using the xlrd module (assume files are imported).

mainData_book = xlrd.open_workbook("IEsummary.xls", formatting_info=True)
mainData_sheet = mainData_book.sheet_by_index(0) # Get the first sheet 0
start = 1
end = 101
for counter in range(start, end):
    rowValues = mainData_sheet.row_values(counter, start_colx=0, end_colx=8)
    company_name = rowValues[0] #how i can get link here also??
like image 793
Aamir Rind Avatar asked Aug 14 '11 12:08

Aamir Rind


People also ask

How do you return a link in Excel?

To navigate back to a hyperlink, after clicking the link, you can use a two step-process: (1) use the shortcut control + G to bring up the Go To dialog box, (2) press Enter to go back to the hyperlink. This works because the Go To dialog records the hyperlink location when a hyperlink is clicked.


1 Answers

In xlrd 0.7.2 or newer, you can use hyperlink_map:

import xlrd
mainData_book = xlrd.open_workbook("IEsummary.xls", formatting_info=True)
mainData_sheet = mainData_book.sheet_by_index(0)
for row in range(1, 101):
    rowValues = mainData_sheet.row_values(row, start_colx=0, end_colx=8)
    company_name = rowValues[0]

    link = mainData_sheet.hyperlink_map.get((row, 0))
    url = '(No URL)' if link is None else link.url_or_path
    print(company_name.ljust(20) + ': ' + url)
like image 190
phihag Avatar answered Oct 08 '22 00:10

phihag