在某些单元格中添加超链接 openpyxl [英] adding hyperlinks in some cells openpyxl

查看:99
本文介绍了在某些单元格中添加超链接 openpyxl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须生成一个带有汇总结果的 excel.结果包含在列表中.一些元素是值和一些链接.

I have to generate an excel with summary results. The results are included in a list. Some of the elements are values and some links.

我设法以正确的格式生成了excel,但没有在某些单元格中生成超链接

I managed to generate the excel with the right format but not generate the hyperlink in some of the cells

我的尝试:从 openpyxl 导入工作簿

My try: from openpyxl import Workbook

from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font, Fill
from openpyxl.cell import get_column_letter

def summaryMCP(self,result):

            c1=Column('Name',[result[0]])
            c2=Column('R2 check',[result[1]])
            c3=Column('Dir Diff.',[result[2]])

            c4=Column('CHI2 Sm-Sc',[result[3]])#Lets say this one is a hyperlink to one image png
            c5=Column('Rose Sm-Sc',[result[4]])

            s=Sheet("MCP main results", [c1,c2,c3,c4,c5]
            excelMCP([s],"/results.xlsx") 

def excelMCP(self, sheets,foname):
            wb = Workbook()
            ws = wb.active
            #from here format options (a bit long)

我的问题是我可以在def summaryMCP中定义列时定义值是超链接,然后在excelMCP中定义链接的格式吗??万一,怎么办?暂时没找到

My question is can I define that the value is a hyperlink when defining the Column in def summaryMCP and then in excelMCP the format of the link?? And in case, how ? I could not find it so far

推荐答案

如果想直接使用Excel自带的超链接功能,可以使用如下格式作为链接:

If wanting to use Excel's built in hyperlink function directly, you can use the following to format as a link:

'=HYPERLINK("{}", "{}")'.format(link, "Link Name")

例如ws.cell(row=1, column=1).value = '=HYPERLINK("{}", "{}")'.format(link, "Link Name")

这篇关于在某些单元格中添加超链接 openpyxl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆