Raghav Shaligram
Raghav Shaligram

Reputation: 309

Adding hyperlink to cell with text using xlwt

I want to add a url as a link below the text in a cell in excel so that user can visit the site. I am trying but its not working. Please help

base = xlwt.Workbook()
for k,v in MainDict.items():
    base.add_sheet(k)
    xlwt.add_palette_colour("custom_colour", 0x21)
    base.set_colour_RGB(0x21, 251, 228, 228)
    style = xlwt.easyxf('pattern: pattern solid, fore_colour custom_colour;font : bold on')
    style1 = xlwt.easyxf('font : underline single')
    index = MainDict.keys().index(k)
    ws = base.get_sheet(index)
    col=0
    for sk in MainDict[k].keys():
        ws.write(0,col,sk.upper(),style)
        col+=1
        row =1
        for mk in MainDict[k][sk].keys():
            for lk,lv in MainDict[k][sk][mk].items():
                for items in lv:
                    ws.write(row,col-1,(items + str(Formula('HYPERLINK("%s")'%mk))))
                    row+=1
base.save('project2.xls')

Upvotes: 3

Views: 2762

Answers (1)

alecxe
alecxe

Reputation: 474021

You need to make the text a part of a Formula, & would help with concatenation. Example:

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('test')

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

wb.save('test.xls')

Upvotes: 2

Related Questions