使用Python在XLSXWriter中创建超链接

发布于 2025-01-27 08:38:55 字数 1029 浏览 5 评论 0原文

我正在尝试在数据集中创建超链接,该数据集最终使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2025-02-03 08:38:55

您的make_hyperlink()函数以及如何应用它中有几个问题。这是一个更正的版本:

import pandas as pd

#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(url):
    return '=HYPERLINK("%s", "Site Link")' % url


#apply function
df['hyperlink'] = df['urls'].apply(make_hyperlink)

#view df
print(df)

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.
writer.close()

输出

”在此处输入图像描述”

There are a couple of issues in your make_hyperlink() function and how you apply it. Here is a corrected version:

import pandas as pd

#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(url):
    return '=HYPERLINK("%s", "Site Link")' % url


#apply function
df['hyperlink'] = df['urls'].apply(make_hyperlink)

#view df
print(df)

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Close the Pandas Excel writer and output the Excel file.
writer.close()

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文