[python]使用openpyxl在一个worksheet中添加跳转到另一个worksheet中的超链接

在使用openpyxl的时候,希望添加一个超链接,从一个worksheet跳转到另一个worksheet中。在stack overflow中找到一个很好的方法,保存下来。

原文链接:

https://stackoverflow.com/questions/30110511/how-to-create-a-hyperlink-to-a-different-excel-sheet-in-the-same-workbook

原文如下:

I found a way to do it.

Assuming one .xlsx file named 'workbookEx.xlsx' with two sheets named 'sheet1' and 'sheet2' and needing a link from one cell(A1) of the 'sheet1' to another cell(E5) of the 'sheet2':

from openpyxl import load_workbook

wb = load_workbook(workbookEx.xlsx) 
ws = wb.get_sheet_by_name("sheet1")

link = "workbookEx.xlsx#sheet2!E5"

ws.cell(row=1, column=1).hyperlink = (link)

The secret was the "#", Excel do not shows you but it uses the '#' for same file links, I just had to copy a same file link created in Excel to a Word document to see the '#'.

It is also possible to omit the filename, i.e. to link against a sheet of the active document just use: _cell.hyperlink = '#sheetName!A1'.

To name the link you just created, just set the cell value to the desired string: _cell.value = 'Linkname'.

补充:

实际上,link中不用添加文件名,默认就是当前文件。

link = "#sheet2!E5"    #默认就是当前文件

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值