wri*_*t17 4 python excel hyperlink pandas
我已经使用将pandas DataFrame 转换为Excel工作表df.to_excel.
现在,我想在一列中添加超值的超链接.换句话说,当客户看到我的Excel工作表时,他就可以点击一个单元格并显示一个网页(取决于此单元格中的值).
Dan*_*nid 13
基于@guillaume-jacquenot 的方法,我们可以将apply其应用于整个系列。
df = pd.DataFrame({'Year': [2000, 2001, 2002 , 2003]})
Run Code Online (Sandbox Code Playgroud)
为了清洁,我写了一个辅助方法。
def make_hyperlink(value):
url = "https://custom.url/{}"
return '=HYPERLINK("%s", "%s")' % (url.format(value), value)
Run Code Online (Sandbox Code Playgroud)
然后,apply它到系列:
df['hyperlink'] = df['Year'].apply(lambda x: make_hyperlink(x))
Run Code Online (Sandbox Code Playgroud)
>
Year hyperlink
0 2000 =HYPERLINK("https://custom.url/2000", "2000")
1 2001 =HYPERLINK("https://custom.url/2001", "2001")
2 2002 =HYPERLINK("https://custom.url/2002", "2002")
3 2003 =HYPERLINK("https://custom.url/2003", "2003")
Run Code Online (Sandbox Code Playgroud)
你可以使用这个HYPERLINK功能
import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("http://www.someurl.com", "some website")']})
df.to_excel('test.xlsx')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7509 次 |
| 最近记录: |