I would be interested to understand your workflow a bit better, but to try and help with your specific request:
- The HYPERLINK solution proposed in the comments looks like a good one
- If you are able to implement that upstream in the csv generation step then great
- If not and/or you are interested in automating the conversion process, consider using the pandas library:
- Create a DataFrame object from a csv using the pandas.read_csv method
- Convert your paths to HYPERLINKs
- Write back to xlsx using the pandas.DataFrame.to_excel method
E.g. if you have a file original.csv and the relevant column header is file_paths:
import pandas as pd
df = pd.read_csv('original.csv')
df['file_paths'] = '=HYPERLINK("' + df['file_paths'] + '")'
df.to_excel('new.xlsx', index=False)
Hope that helps!
Jon
Answer from Jon Hannah on Stack OverflowInsert hyperlink to a local folder in Excel with Python - Stack Overflow
python - adding hyperlinks in some cells openpyxl - Stack Overflow
Trying to open a hyperlink in excel with python
python - Create a hyperlink to a different Excel sheet in the same workbook - Stack Overflow
The XlsxWriter write_url() method allows you to link to folders or other workbooks and worksheets as well as internal links and links to web urls. For example:
import xlsxwriter
workbook = xlsxwriter.Workbook('links.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('A:A', 50)
# Link to a Folder.
worksheet.write_url('A1', r'external:C:\Temp')
# Link to a workbook.
worksheet.write_url('A3', r'external:C:\Temp\Book.xlsx')
# Link to a cell in a worksheet.
worksheet.write_url('A5', r'external:C:\Temp\Book.xlsx#Sheet1!C5')
workbook.close()
See the docs linked to above for more details.
Here is the code that did the trick:-
# Creates hyperlink in existing workbook...
def set_hyperlink():
from openpyxl import load_workbook
x = "hyperlink address"
wb = load_workbook("filename.xlsx")
ws = wb.get_sheet_by_name("sheet_name")
ws.cell(row = x?, column = y?).hyperlink = x
wb.save("filename.xlsx")
set_hyperlink()
Tried again with openpyxl as advised.
This works for me:
wbook.active['A8'].hyperlink = "http://www.espn.com"
wbook.active['A8'].value = 'ESPN'
wbook.active['A8'].style = "Hyperlink"
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")
Hello,
I have an excel file with hyperlinks (these hyperlinks lead to a website). I want to python to open one of the hyperlinks. When I execute the script I would expect the websites to open in Microsoft edge, but instead, nothing happens. Here is my code:
import openpyxl
wb = openpyxl.load_workbook("C:\\Users\\rfog\\OneDrive\\Documents\\Python Excel\\pythontest.xlsx")
ws = wb['Sheet1']
ws.cell(row=1, column=2).hyperlink.target
Any suggestions?
I found a way to do it.
Assuming one .xlsx file named 'workbookEx.xlsx' with two sheets named 'sheet1' and 'sheet2' and needing a link from one cell(A1) of the 'sheet1' to another cell(E5) of the 'sheet2':
from openpyxl import load_workbook
wb = load_workbook(workbookEx.xlsx)
ws = wb.get_sheet_by_name("sheet1")
link = "workbookEx.xlsx#sheet2!E5"
ws.cell(row=1, column=1).hyperlink = (link)
The secret was the "#", Excel do not shows you but it uses the '#' for same file links, I just had to copy a same file link created in Excel to a Word document to see the '#'.
It is also possible to omit the filename, i.e. to link against a sheet of the active document just use: _cell.hyperlink = '#sheetName!A1'.
To name the link you just created, just set the cell value to the desired string: _cell.value = 'Linkname'.
As an addendum to Marcus.Luck's answer, if wanting to use Excel's built-in hyperlink function directly, you may need to format as:
'=HYPERLINK("{}", "{}")'.format(link, "Link Name")
Without this formatting, the file didn't open for me without needing repair, which removed the cell values when clicking the links.
e.g. ws.cell(row=1, column=1).value = '=HYPERLINK("{}", "{}")'.format(link, "Link Name")