Unfortunately, this is a mistake.
This is a mistake in 2012 ...
Some related threads:
Extract hyperlinks from Excel (.xlsx) using Python
Some details of my experiment with a hyperlink. I am using OpenPyXL 2.3.3 .
- I can add a hyperlink to the cells.
from openpyxl import load_workbook xlsFile='hello.xlsx' wbook = load_workbook(xlsFile) wsheet1= wbook.get_sheet_by_name('mysheet') cell1 = wsheet1.cell('A1') cell1.hyperlink = r'http://www.example.com' cell1.value=r'XXX' wbook.save(xlsFile)
But I cannot download the XLSX file and read the hyperlink as my question said.
And if I just download and save the XLSX file , all existing hyperlinks will be lost. Yes!
from openpyxl import load_workbook xlsFile='hello.xlsx' wbook = load_workbook(xlsFile) wbook.save(xlsFile)
Workaround!
Use the formula with OpenPyXL .
My goal is append clickable cells to existing XLSX file . Because hyperlink not working. Instead, I use the formula =HYPERLINK(url, displayText) . And, fortunately, the formula is not lost, like the previous experiment.
from openpyxl import load_workbook xlsFile='hello.xlsx' wbook = load_workbook(xlsFile) wsheet1= wbook.get_sheet_by_name('mysheet') cell1 = wsheet1.cell('A2') cell1.value=r'=HYPERLINK("http://www.example.com","XXX")' wbook.save(xlsFile)
Other (unsuccessful) options I tried:
I looked at XlsxWriter . But he clearly says he cannot modify an existing XLSX file . Therefore, it cannot be used to add.
I also looked at xlrd/xlwt/xlutils , unfortunately, if you want to edit an existing excel, you have to use xlrd to download it as a book to read, and then use xlutils to convert (copy) it to a writable workbook. And BANG! During copying, something will be lost, which includes the hyperlink formula. According to its doc line, this is a known limitation:
# Copyright (c) 2009-2012 Simplistix Ltd
In addition, xlwt does not support XLSX, only supports XLS. This is another reason why I decided not to use it.