我在xlsx電子表格中使用以下代碼片段進行超鏈接。超鏈接無法通過openpyxl工作
- 讀取文件名從H2,H3,H4 XLSX ...
- 搜索在當前文件夾中的文件(其中腳本運行)
- 創建與現有內容搜索路徑超鏈接。
問題是openpyxl .hyperlink甚至沒有與= HYPERLINK( 「路徑」, 「真正的文件名」)寫入正在提前
感謝。
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);
看起來你正在使用一個公式來創建超鏈接; openpyxl不會評估公式,但支持單元格的超鏈接。 –
@CharlieClark如果你看看我的評論腳本#ws.cell(row = RowNum,column = ColumnNum).hyperlink = link;最初用於通過OpenpyXL進行超鏈接而沒有起作用,所以我去了其他的替代方法。 – Lucky
@Lucky你打算使用'xlsxwriter'來代替嗎?謝謝。 – alecxe