使用Python的xlwt模块添加到另一个单元格的链接

使用Python的xlwt模块添加到另一个单元格的链接,python,xlwt,Python,Xlwt,我正在使用用于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,

我正在使用用于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")
# 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))
sheet = wbk.add_sheet('Type ' + str(i)) 
然而,我实际上想做的是“钻取”文档。例如,我希望“sheet1”中的单元格A1指向“sheet3”中的单元格F5


有人知道我的要求是否可行吗;如果是这样的话,我必须使用什么语法来实现这一点?

您可能需要这样的东西:

    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")
# 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))
sheet = wbk.add_sheet('Type ' + str(i)) 
作为:

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

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

如果已按以下方式创建图纸:

    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")
# 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))
sheet = wbk.add_sheet('Type ' + str(i)) 
您可能会发现,使超链接正常工作的唯一方法是添加单个引号(转义)


无论excel中的语法是什么,您都不能使用与上面相同的方法吗?好的,原始海报特别要求使用超链接,而不仅仅是使用另一个单元格的公式。getting FormulaParseException:Can not ParseFormula这只包括工作表名称包含空格的情况。像
O'Reilly's data
这样的工作表名称需要作为
'O'Reilly's data
输入,即遵循与SQL中字符串文字相同的过程:将字符串中的每个撇号加倍,然后在开始和结束处添加撇号。这最好使用
xlwt.Utils.quote\u sheet\u name(unquoted\u sheet\u name)
完成,它执行所需的引用,并检查输入的有效性。检查的基本原理是,最好在文件创建时获取一条合理的错误消息,而不是在(稍后)文件打开时从Excel获取一条乱码消息。