I've been using openpyxl's append() function to add url links to an Excel document. When I open Excel, none of the links have hyperlinks until I go into each individual cell and press enter. How do I format as hyperlink via Python during my append? Thanks!
url = 'www.google.com'
ws.append([url])
CodePudding user response:
You need to set the cell hyperlink attribute and the style. Then Excel doesn't need to work it out, which is what happens when you select the cell and press Enter.
Here's a minimal example:
from openpyxl import Workbook
wb = Workbook() # create a new workbook
ws = wb.active # get the active sheet
ws.title = "Hyperlink Test"
# write to the first cell
cell = ws.cell(column=1, row=1, value="Google")
cell.hyperlink = "https://www.google.com"
cell.style = "Hyperlink"
output_name = "test_hyperink.xlsx"
wb.save(output_name)