[英]adding hyperlinks in some cells openpyxl
I have to generate an excel with summary results.我必须生成一个带有汇总结果的excel。 The results are included in a list.结果包含在列表中。 Some of the elements are values and some links.一些元素是值和一些链接。
I managed to generate the excel with the right format but not generate the hyperlink in some of the cells我设法以正确的格式生成了excel,但没有在某些单元格中生成超链接
My try: from openpyxl import Workbook我的尝试:从 openpyxl 导入工作簿
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)
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??我的问题是,在 def summaryMCP 中定义列时,我可以定义该值是超链接,然后在 excelMCP 中定义链接的格式吗?? And in case, how ?万一,怎么办? I could not find it so far到目前为止我找不到它
If wanting to use Excel's built in hyperlink function directly, you can use the following to format as a link:如果想直接使用Excel内置的超链接功能,可以使用如下格式作为链接:
'=HYPERLINK("{}", "{}")'.format(link, "Link Name")
eg ws.cell(row=1, column=1).value = '=HYPERLINK("{}", "{}")'.format(link, "Link Name")
例如ws.cell(row=1, column=1).value = '=HYPERLINK("{}", "{}")'.format(link, "Link Name")
This works for me:这对我有用:
wbook.active['A8'].hyperlink = "http://www.espn.com"
wbook.active['A8'].value = 'ESPN'
wbook.active['A8'].style = "Hyperlink"
Try this :试试这个:
sheet['A1'].hyperlink = "http://stackoverflow.com"
sheet['A1'].value="StackOverflow"
In my try this does not add the formatting that Excel puts with a hyperlink but the cell contents are more like the Excel hyperlink cell than I get with the HYPERLINK tag.在我的尝试中,这不会添加 Excel 使用超链接放置的格式,但单元格内容更像是 Excel 超链接单元格,而不是我使用 HYPERLINK 标记获得的内容。
This worked for me.这对我有用。
sheet.column_dimensions["A"].width = 30
sheet.cell(row=1, column=1).hyperlink = "http://www.espn.com"
book.save('links.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.