简体   繁体   English

使用xlwt添加带有文本的单元格的超链接

[英]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. 我想在excel的单元格中的文本下面添加一个URL作为链接,以便用户可以访问该站点。 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')

You need to make the text a part of a Formula , & would help with concatenation. 你需要做的文本的一部分Formula&将与串联帮助。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM