如何在 pandas 中处理带有 hyperlink/url 的 excel 文件?

how do I process an excel file with hyperlink/url in pandas?

我有一个 excel 文件,其中有一列充满了超链接,我使用 df = pd.read_excel() 读取它,然后过滤它并使用 [=14] 将其保存到新的 excel 文件中=].

问题是我现在丢失了可点击的超链接,取而代之的是文本(不是超链接)

我可以使用 pandas 吗?还是我应该使用其他库?

您可以使用 import xlsxwriter 库来添加超链接。说到超链接,示例 here 显示了一些示例,例如:

worksheet.write_url('A5', 'http://www.python.org/', tip='Click here')

但是,如果您不想为每个单元格手动编写一行代码,那么如果您有所有超链接的列表,则可以循环并动态添加超链接。

hyperlinks = ['a.com', 'b.com', 'c.com' ... etc.]

for i in range(1, len(hyperlinks)):
    worksheet.write_url(f'A{i}', hyperlinks[i-1], tip=df['column string'][i-1])

您的超链接显然必须在 list 中以正确的顺序排列,或者您可以创建一个 dictionary 使文本和超链接成为 key-value 对并使用 .map 将超链接作为一列放入您的数据框中。然后您可以对值进行排序并将超链接发送到带有 hyperlinks = df['hyperlink'].to_list() 的列表。然后你可以 运行 for-loop.

但是,我认为您必须先创建一个列表或字典。


此外,使用 openpyxl:

查看此答案以使用超链接阅读日期

Pandas read_excel with Hyperlink

而且,这个用于使用 pandas 写入带有超链接的数据:

add hyperlink to excel sheet created by pandas dataframe to_excel method