如何在Excel中使用超链接链接到python中同一工作簿中的另一个工作表单元格

时间:2016-10-30 06:22:32

标签: python excel

如何链接到excel中同一工作簿中的另一个工作表

这个python脚本不是

from openpyxl import load_workbook

wb = load_workbook("excel_hyper_link_test.xlsx") 
ws = wb.get_sheet_by_name("Sheet1")

link = "excel_hyper_link_test.xlsx#Sheet2!E5"
print ws.cell(row=1, column=1).value

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

运行此脚本后,我打开了excel表,我看不到任何超链接

注意:我使用的是linux平台

2 个答案:

答案 0 :(得分:0)

这对我有用

from openpyxl import load_workbook

xlsFile='excel_hyper_link_test.xlsx'
wbook = load_workbook(xlsFile)
wsheet1= wbook.get_sheet_by_name('Sheet1')
cell1 = wsheet1.cell('A1')
cell1.hyperlink = '#Sheet2!E5'
cell1.value=r'XXX'
wbook.save(xlsFile)

答案 1 :(得分:0)

  import pandas as pd
    import openpyxl as opxl
    def hyperlinking(New_file_path):
        xls = pd.ExcelFile(New_file_path)
        sheets = xls.sheet_names # Get the worksheet names
        wb = opxl.load_workbook(New_file_path)
        ws = wb.create_sheet("Consolitated_Sheet") # Create a New worksheet
        ws['A1'] = "Sheet_Name"; ws['B1'] = "Active_link" #New sheet we are proving column names
        for i, j in enumerate(sheets):
            # print('A'+str(i+2) + ' value is: ' + j)
            ws['A' + str(i + 2)] = j # As A1 cell is occupied with column name we are taking reference of second row(A2).
            ws['B' + str(i + 2)].value = '=HYPERLINK("%s", "%s")' % ('#' + str(j) + '!A1', 'Clickhere') # For A2 cell value we are providing hyperlinks of respective sheet
        wb.save(New_file_path)
        wb.close()