nooblar
nooblar

Reputation: 419

Adding links to another cell using the xlwt module for Python

I am exporting some information to an excel workbook using the awesome xlwt module for Python. I know that I can have a certain cell contain a hyperlink that points to an external site like this:

    from xlwt import Workbook, Formula
    wb = Workbook()
    sheet = wb.add_sheet('testing links')
    link = 'HYPERLINK("http://stackoverflow.com/"; "SO")'
    sheet.write(0, 0, Formula(link))
    wb.save("testbk.xls")

However, what I actually want to do is something like "drilling through" the document. I want cell A1 from "sheet1" to point to cell F5 in "sheet3" for example.

Does someone know if what I am asking is possible; and if so, what syntax I must use to accomplish that?

Upvotes: 4

Views: 4779

Answers (3)

oden
oden

Reputation: 3651

If you have created the sheet like this:

sheet = wbk.add_sheet('Type ' + str(i)) 

You may find the only way to get the hyperlink to work is to add a single quote (escaped)

link = 'HYPERLINK("#\'Type '+ str(i) + '\'!A1", "Link")'
sheet.write(0, 1, xlwt.Formula(link))

Upvotes: 2

John Machin
John Machin

Reputation: 82992

As answered on the python-excel forum:

STW to find out how a user does it in Excel:

=HYPERLINK("#Sheet3!F5","some text")

Upvotes: 7

cwallenpoole
cwallenpoole

Reputation: 82058

You'd probably want something like this:

# to get to a different sheet in XL, use the sheet name, an !, and then the 
# cell coordinates. In this case, you're going to sheet3, cell F3
link = 'sheet3!F3'
# This is still a formula, so you should link it as such.
sheet.write(0, 0, xlwt.Formula(link))

Upvotes: 3

Related Questions