Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding hyperlinks in some cells openpyxl

Tags:

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.

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

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)

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

like image 265
gis20 Avatar asked Aug 22 '16 10:08

gis20


People also ask

How do you hyperlink to a specific cell?

On a worksheet, select the cell where you want to create a link. On the Insert tab, select Hyperlink. You can also right-click the cell and then select Hyperlink... on the shortcut menu, or you can press Ctrl+K.

How do I merge cells in openpyxl?

Implementation Of Merging Cells using openpyxl Openpyxl provides a merge_cells() function for merging cells.


1 Answers

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")

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

like image 174
Phillip Avatar answered Sep 18 '22 15:09

Phillip