超链接不能通过openpyxl工作

我在xlsx电子表格中使用以下代码片段进行超级链接。

  1. 从H2,H3,H4从xlsx读取文件名
  2. search当前文件夹中的文件(脚本正在运行)
  3. 用现有内容创build与searchpath的超链接。

问题是。从超级链接到openpyxl甚至没有写= HYPERLINK(“path”,“真实文件名”)正在工作

提前致谢。

import os import openpyxl ColumnNum = 6 RowNum = 2 rootPath = "" def FindPathofFile(filename): for root, dirs, files in os.walk(rootPath): for file in files: if filename in file: return(os.path.join(root, file)) rootPath =input("Enter the Parent Path, Where the html files are present\n"); SpreadSheetName = input("Enter the SwCTS spread sheet name, in which Hyperlinks to be created\n"); wb = load_workbook(SpreadSheetName); ws = wb.get_sheet_by_name(input("Enter the SwCTS Tab Name, in which Hyperlinks to be created\n")); columnname = "H"+str(RowNum); valueofCell = ws[columnname].value; while True: if valueofCell: link = FindPathofFile(valueofCell); print ('=HYPERLINK("'+str(link)+'","'+str(valueofCell)+'")'); #ws.cell(row=RowNum, column=ColumnNum).hyperlink = link; ws.cell(row=RowNum, column=ColumnNum).value ='=HYPERLINK("'+str(link)+'","'+str(valueofCell)+'")'; RowNum = RowNum + 1; columnname = "H"+str(RowNum); valueofCell = ws[columnname].value; else: break; wb.save(SpreadSheetName); 

那么,我可以到达这一点。 虽然没有直接的方式来build立一个超链接,在你的情况下,我们可以这样做。 事实上,我没有检查你的程序的彻底性。 我相信你知道你还在做什么。 我能够使用下面的代码build立到现有文件的超链接。 我看到,我所拥有的和你所拥有的只有一点逻辑上的区别。 即“风格”属性的缺失。

 wb=openpyxl.Workbook() s = wb.get_sheet_by_name('Sheet') s['B4'].value = '=HYPERLINK("C:\\Users\\Manoj.Waghmare\\Desktop\\script.txt", "newfile")' s['B4'].style = 'Hyperlink' wb.save('trial.xlsx') 

通过提到风格属性作为“超链接”是关键。 我拥有的所有其他代码对您来说可能并不重要。 style属性会有一个'Normal'的值奇怪的是即使没有style属性,我们工作的超链接也只是它缺乏风格! 当然。 虽然很奇怪,但我看到了陌生的东西。 希望这可以帮助。

@Lucky,找不到任何创build超链接的失败。

下面所有的Methodes将一个超链接写入一个xlsx文件:

 from openpyxl.workbook import Workbook wb = Workbook() ws = wb.worksheets[0] link = 'http://www.example.org' linkText = 'Click here' 

Methode 1

 ws.cell(row=2, column=6).hyperlink = link 

在单元格中显示“ http://www.example.org ”,并单击时打开超链接http://www.example.org

Methode 2

 ws.cell(row=4, column=6).hyperlink = link ws.cell(row=4, column=6).value = linkText 

在单元格中显示“ Click here ”, 单击时打开超链接http://www.example.org


Methode 3使用公式= HYPERLINK(link,cellText)

例1:

 ws.cell(row=6, column=6).value = '=HYPERLINK("http://www.example.org")' 

在单元格中显示“ http://www.example.org ”,并单击时打开超链接http://www.example.org

例2:

 ws.cell(row=8, column=6).value = '=HYPERLINK ( "http://www.example.org", "Click here" )' 

在单元格中显示“ Click here ”, 单击时打开超链接http://www.example.org

例3:

 ws.cell(row=10, column=6).value = '=HYPERLINK("http://www.", "Click ") & "example.org"' 

在单元格中显示“ Click example.org ”, 单击时打开超链接http://www.example.org

用Pythontesting:3.4.2 – openpyxl:2.4.1 – LibreOffice:4.3.3.2