使用Python在XLSXWriter中创建超链接
我正在尝试在数据集中创建超链接,该数据集最终使用python脚本中创建的函数最终使用XLSWRITER导出。但是,产生的输出并不如预期。
= Excel中的Hyperlink()
需要两个参数。这些是目的地URL和锚文字。这就是我试图使用XLSXWRITER在Python中生产的,以便在导入时,Excel可以将列读为链接。
请参阅下面的Exmaple。
#create exmaple df
df = pd.DataFrame({'urls': ['https://www.google.com', 'https://www.youtube.com', 'https://www.twitter.com','https://www.reddit.com']})
#create function
def make_hyperlink(x):
for urls in df['urls']:
return '=HYPERLINK(' + urls, "Site Link"
#apply function
df['hyperlink'] = df['urls'].apply(lambda x: make_hyperlink(x))
#view df
df
输出:
urls hyperlink
0 https://www.google.com (=HYPERLINK(https://www.google.com, Site Link)
1 https://www.youtube.com (=HYPERLINK(https://www.google.com, Site Link)
2 https://www.twitter.com (=HYPERLINK(https://www.google.com, Site Link)
3 https://www.reddit.com (=HYPERLINK(https://www.google.com, Site Link)
我希望输出以“站点链接”为锚文本显示每个单个URL,因此我不确定为什么该函数仅适用于第一个URL,而是四次。
指导赞赏。
I'm trying to create hyperlinks in an dataset that is eventually exported using xlswriter using a function I create in my python script. However, the output produced is not as expected.
=HYPERLINK()
in Excel takes two arguments. These are the destination URL and the anchor text. It is this that I am trying to produce in Python using XLSXWriter so that when imported, Excel is able to read column as a link.
See exmaple below.
#create exmaple df
df = pd.DataFrame({'urls': ['https://www.google.com', 'https://www.youtube.com', 'https://www.twitter.com','https://www.reddit.com']})
#create function
def make_hyperlink(x):
for urls in df['urls']:
return '=HYPERLINK(' + urls, "Site Link"
#apply function
df['hyperlink'] = df['urls'].apply(lambda x: make_hyperlink(x))
#view df
df
output:
urls hyperlink
0 https://www.google.com (=HYPERLINK(https://www.google.com, Site Link)
1 https://www.youtube.com (=HYPERLINK(https://www.google.com, Site Link)
2 https://www.twitter.com (=HYPERLINK(https://www.google.com, Site Link)
3 https://www.reddit.com (=HYPERLINK(https://www.google.com, Site Link)
I am expecting the output to display each of the individual URLs with 'Site Link' as the anchor text so I am unsure as to why the function only applies to the first URL but four times.
Guidance appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
make_hyperlink()
函数以及如何应用它中有几个问题。这是一个更正的版本:输出:
There are a couple of issues in your
make_hyperlink()
function and how you apply it. Here is a corrected version:Output: