带超链接的 Pandas read_excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35325799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:40:31  来源:igfitidea点击:

Pandas read_excel with Hyperlink

excelpandas

提问by slaw

I have an Excel spreadsheet that I am reading into a Pandas DataFrame:

我有一个 Excel 电子表格,我正在将它读入 Pandas DataFrame:

df = pd.read_excel("file.xls")

However, one of the columns of the spreadsheet contains text which have a hyperlink associated with it. How do I access the underlying hyperlink in Pandas?

但是,电子表格的其中一列包含具有与之关联的超链接的文本。如何访问 Pandas 中的底层超链接?

回答by wordsforthewise

This can be done with openpyxl, I'm not sure its possible with Pandas at all. Here's how I've done it:

这可以通过 openpyxl 完成,我不确定 Pandas 是否可行。这是我如何做到的:

import openpxyl

wb = openpyxl.load_workbook('yourfile.xlsm')
ws = wb.get_sheet_by_name('Sheet1')
print(ws.cell(row=2, column=1).hyperlink.target)

You can also use iPython, and set a variable equal to the hyperlink object:

您还可以使用 iPython,并设置一个等于超链接对象的变量:

t = ws.cell(row=2, column=1).hyperlink

t = ws.cell(row=2, column=1).hyperlink

then do t.and press tab to see all the options for what you can do with or access from the object.

然后执行t.并按 Tab 键查看您可以使用该对象执行的操作或访问该对象的所有选项。

回答by rohitphilonoist

as commented by slaw it doesnt grab the hyperlink but only the text

正如 slaw 所评论的,它不会抓取超链接,而只会抓取文本

here text.xlsx contains links in the 9th column

这里 text.xlsx 包含第 9 列中的链接

workbook = load_workbook('test.xlsx')
worksheet = workbook.active

column_indices = [9]

for row in range(2,worksheet.max_row+1): 
    for col in column_indices:
        filelocation = worksheet.cell(column=col, row=row ) #this is hyperlink
        text = worksheet.cell(column=col+1, row=row) # thi is your text 
        worksheet.cell(column=col+1,row=row).value = '=HYPERLINK("'+filelocation.value+'","'+text.value+'")'


workbook.save('test.xlsx')

回答by Stop harming Monica

You cannot do that in pandas. You can try with other libraries designed to deal with excel files.

你不能在Pandas中做到这一点。您可以尝试使用其他旨在处理 excel 文件的库