• 技术文章 >Python技术 >Python基础教程

    Python实用之xlwt单元格中加超链接

    2020-11-05 15:26:57原创8148

    平时给文本加超链接相信大家都会,在表格中用xlwt就难倒了,今天我们一起来学习吧。

    问题:


    在excel单元格中的文本下方添加一个URL作为链接,以便用户可以访问该网站,但无法运行。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">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')<br></span></p>


    解决:

    你需要使文本的一部分Formula&将与串联帮助。例:


    1

    2

    3

    4

    5

    6

    7

    8

    <p style="line-height: 1.75em;"><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px;">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')<br></span></p>

    以上就是xlwt单元格中加超链接的方法啦。更多Python学习推荐:PyThon学习网教学中心

    专题推荐:xlwt单元格加超链接;python
    上一篇:关于Python条件控制怎么用?要注意什么? 下一篇:Python max() 函数怎么用?什么原理?

    相关文章推荐

    • python中xlwt是什么• Python中如何用xlwt制作表格• Python中如何安装xlwt• Python中xlwt如何设置单元格格式

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网