openpyxl的cell有属性hyperlink属性,这个属性可以设置超链接,如果只是想设置一列有超链接,可以用if来设置
def write_to_execl_link(filename = './新建.xlsx',title='sheet',sheet_header = [],sheet_data = []):
import openpyxl
wb = openpyxl.Workbook()
ws = wb.active
ws.title = title
row0 = sheet_header
for i, r in enumerate(row0):
ws.cell(row=1, column=i + 1, value=r)
for i, r in enumerate(sheet_data):
for j, c in enumerate(r):
# if i%2 == 1:
# ws.cell(row=i + 2, column=j + 1).fill = fill
ws.cell(row=i + 2, column=j + 1, value=c)
ws.cell(row=i + 2, column=j + 1).hyperlink = r[-1]
wb.save(filename)
openpyxl获取文件内容的超链接
import openpyxl
import os
#将Excel文件放在python同级目录
dir_path = os.path.dirname(os.path.realpath(__file__))
test_xlsx = os.path.join(dir_path,f'''test.xlsx''')
wb = openpyxl.load_workbook(test_xlsx)
sheet = wb.active
print(sheet.cell(1, 1).value)
print(sheet.cell(1, 1).hyperlink.target)