超链接无法通过openpyx工作

2024-10-01 15:32:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  1. 从xlsx从H2,H3,H4读取文件名。。。在
  2. 在当前文件夹(运行脚本的位置)中搜索文件
  3. 使用现有内容创建具有搜索路径的超链接。在

问题是,来自openpyxl的超链接,甚至不使用=hyperlink(“Path”,“Real File Name”)进行写操作

提前谢谢。在

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);


Tags: theininputwsoslinkfilesfile
2条回答

@Lucky,未发现创建超链接失败。在

以下所有方法都会将工作超链接写入xlsx文件:

from openpyxl.workbook import Workbook
wb = Workbook()
ws = wb.worksheets[0]

link = 'http://www.example.org'
linkText = 'Click here'

方法1

^{pr2}$

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

方法2

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

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


方法3 (使用公式=超链接(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" )'

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

示例3:

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

显示'单击示例.org,并在单击时打开超链接http://www.example.org。在

测试Python:3.2-openpyxl:2.4.1-图书馆办公室:4.3.3.2

好吧,我可以做到这一点。虽然没有直接的方法来建立一个超链接,在你的情况下,我们可以这样做。事实上,我没有检查你的计划是否彻底。我相信你还知道你在做什么。我能够建立一个超链接到一个现有的文件使用下面的代码。我知道我和你之间只有一点逻辑上的区别。i、 “风格”属性的缺失。在

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” 奇怪的是即使没有样式属性,我们工作的超链接也只是缺少样式!当然。虽然很奇怪,但我看到了一些奇怪的东西。希望这有帮助。在

相关问题 更多 >

    热门问题