ballade4op52
ballade4op52

Reputation: 2267

Can xlwt create a hyperlink in a cell with variables for both the title and link?

For instance,

How would the following line be changed such that "test" is variable T and "http://google.com" is variable L?

ws.write(0, 0, xlwt.Formula('"test " & HYPERLINK("http://google.com")'))

Upvotes: 0

Views: 2142

Answers (1)

mhawke
mhawke

Reputation: 87124

Try this:

T = 'test'
L = 'http://google.com'
formula = '"{} " & HYPERLINK("{}")'.format(T, L)
ws.write(0, 0, xlwt.Formula(formula))

This uses str.format() to insert the values of variables T and L in to the string formula. Following the above assignments formula will contain:

"test " & HYPERLINK("http://google.com")

It's also possible to do it without using the temporary variable:

ws.write(0, 0, xlwt.Formula('"{} " & HYPERLINK("{}")'.format(T, L)))

Update

The code above answers the question as it was stated, however, the OP actually requires that "test" be displayed as the link and the URL be the target, so:

T = 'test'
L = 'http://google.com'
formula = 'HYPERLINK("{}", "{}")'.format(L, T)
ws.write(0, 0, xlwt.Formula(formula))

>>> formula
'HYPERLINK("http://google.com", "test")'

Upvotes: 2

Related Questions