openpyxl设置excel跳转链接 和跳转样式

from openpyxl import load_workbook


# 设置excel的链接 包含样式
def sheet_link(dir_name, sheet1, row1, column, sheet2, position):
    """
    :param dir_name:
    :param sheet1: 初始发起跳转的sheet表单名
    :param row1: 跳转的行 int类型
    :param column: 跳转的列 int类型
    :param sheet2: 目标sheet
    :param position: 目标位置 如:E1
    :return:
    """
    xlsFile = dir_name
    wbook = load_workbook(xlsFile)
    wsheet1 = wbook[sheet1]
    cell1 = wsheet1.cell(row=row1, column=column)
    target = '#' + sheet2 + '!' + position
    cell1.hyperlink = target
    # 设置连接样式
    cell1.style = "Hyperlink"
    wbook.save(xlsFile)

你可能感兴趣的:(笔记,python)