首页 文章

使用xlwt模块为Python添加指向另一个单元的链接

提问于
浏览
4

我正在使用Python的超棒xlwt模块将一些信息导出到excel工作簿 . 我知道我可以让某个单元格包含指向外部网站的超链接,如下所示:

from xlwt import Workbook, Formula
    wb = Workbook()
    sheet = wb.add_sheet('testing links')
    link = 'HYPERLINK("http://stackoverflow.com/"; "SO")'
    sheet.write(0, 0, Formula(link))
    wb.save("testbk.xls")

但是,我真正想要做的就是“钻取”文档 . 我希望“sheet1”中的单元格A1指向“sheet3”中的单元格F5 .

有人知道我问的是否可能;如果是这样,我必须使用什么语法来实现这一目标?

3 回答

  • 2

    answered on the python-excel forum

    STW了解用户如何在Excel中执行此操作:

    =HYPERLINK("#Sheet3!F5","some text")
    
  • 3

    你可能想要这样的东西:

    # to get to a different sheet in XL, use the sheet name, an !, and then the 
    # cell coordinates. In this case, you're going to sheet3, cell F3
    link = 'sheet3!F3'
    # This is still a formula, so you should link it as such.
    sheet.write(0, 0, xlwt.Formula(link))
    
  • 7

    如果您已经创建了这样的表:

    sheet = wbk.add_sheet('Type ' + str(i))
    

    您可能会发现使超链接工作的唯一方法是添加单引号(转义)

    link = 'HYPERLINK("#\'Type '+ str(i) + '\'!A1", "Link")'
    sheet.write(0, 1, xlwt.Formula(link))
    

相关问题