Search code examples
pythonxlwt

xlwt use Hyperlink with variable


I would like to use xlwt to generate hyperlink in particular cell. I tried to put following in formula, it's fine:

Hyperlink("http://www.google.com";"Link") 

But when I define X='"http://www.google.com"' (Note the single quote outside of double quote) and then: Hyperlink(X;"Link") It won't work.

Basically, I want to put a variable X, which could be different when the program runs, into Hyperlink(). Any idea to fix this problem would be appreciated!


Solution

  • Use this construct

    click='"http://www.google.com"'
    wsheet.write(j,8,xlwt.Formula('HYPERLINK(%s;"Link")' % click))
    

    or, easier to read and maintain:

    click='http://www.google.com'
    wsheet.write(j,8,xlwt.Formula('HYPERLINK("%s";"Link")' % click))
    

    For details of the % operator for string formatting, see http://docs.python.org/2/library/stdtypes.html#string-formatting