Adding hyperlinks to some openpyxl cells

I need to create excel with the final results. Results are listed. Some of the elements are meanings and some links.

I managed to create excel with the correct format but not generate hyperlink in some cells

My attempt: 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)

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

+11
source share
4 answers

Excel , :

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

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

+10

:

sheet['A1'].hyperlink = "http://stackoverflow.com"
sheet['A1'].value="StackOverflow"

, Excel , Excel, HYPERLINK.

+7

This works for me:

wbook.active['A8'].hyperlink = "http://www.espn.com"
wbook.active['A8'].value = 'ESPN'
wbook.actibe['A8'].style = "Hyperlink"
+3
source

It worked for me.

sheet.column_dimensions["A"].width = 30
sheet.cell(row=1, column=1).hyperlink = "http://www.espn.com"
book.save('links.xlsx')
+3
source

Source: https://habr.com/ru/post/1652033/


All Articles