user987654321
user987654321

Reputation: 303

Add link to text within cell, not entire cell

Using Openpyxl, is there a way to create a link within a cell?

I tried:

worksheet['A1'].hyperlink = 'http://mypage.com'

However, this sets the entire cell of 'A1' to be a link. I would like it to set the text within the cell to a link so that it looks like: My page in cell A1.

Upvotes: 0

Views: 594

Answers (1)

tpearse
tpearse

Reputation: 406

You can try something like this:

wb = load_workbook("my_book.xlsx")
worksheet1 = wb.active()
cell_value = '=HYPERLINK("http://mypage.com", "My Page")'
worksheet1.cell(row=1, column=1, value=cell_value)

The important part of my example is that you can just set the value of the cell to excel's hyperlink function as a string. The first parameter is the link and the second parameter is the text to display in the cell.

Upvotes: 1

Related Questions