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

查看:5112
本文介绍了在某些单元格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 import工作簿

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(链接,链接名称)

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

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

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