SoFunction
Updated on 2024-11-16

python use openpyxl to realize the way to create hyperlinks to excel table relative paths

Use openpyxl to realize the creation of hyperlinks to the relative paths of excel tables.

 # This is a relative path, you can change the parent folder (the folder name images can not be changed)
 # img_path: the name of the image in the images path
(row=img_site1_2 + 1, column=img_site2_2).hyperlink = 'images\\' + img_path

Other relevant codes

# Setting up a centered display against the bottom
    align2 = Alignment(horizontal='right', vertical='justify')
    try:
    	# Progress bar display using tqdm library
        with tqdm(iterable=images, desc='Picture Import', unit='Zhang', total=len(images)) as pgbr:
            for img_path in images:
                # Process image names (get insertion address)
                img_sites = img_path.split('_')
                # of rows (+1 required)
                img_site1 = int(img_sites[0])
                img_site1_2 = img_site1
                # sheet.row_dimensions[img_site1+1].height=40
                img_site1 = str(img_site1+1)
                # Converted to int to make it easier to use this value as an array subscript to retrieve the value of clos, columns
                img_site2 = int(img_sites[1])
                # if img_site2 == 0:
                #     img_site2 = int(img_sites[2])
                img_site2_2 = img_site2 + 11
                # Arrays take values starting at 0 and get values starting at 1, true column values
                img_site2 = cols[img_site2 - 1]
                # The real address of the picture
                img_path_real = savepath2 + '\\' + img_path
                # Insert image local address
                # This is an absolute path (it won't work on a different computer)
                # file_name = 'file:///' + img_path_real # This is redundant, link generation automatically generates the
                # Use relative paths, another computer can access the pictures normally)
                sheet.column_dimensions[img_site2].width=18
                # sh = sheet.row_dimensions[img_site1_2+1].height
                # print("sh: ",sh)
                # inserted into excel
                position = img_site2 + img_site1
                # This is an absolute path, change the folder name and it won't work
                # (row=img_site1_2 + 1, column=img_site2_2).hyperlink = img_path_real 
                # This is a relative path, you can change the parent folder (the folder name images can not be changed)
                (row=img_site1_2 + 1, column=img_site2_2).hyperlink = 'images\\' + img_path
                (row=img_site1_2 + 1, column=img_site2_2).style = "Hyperlink"
                (row=img_site1_2 + 1, column=img_site2_2).value = 'Details'
                (row=img_site1_2 + 1, column=img_site2_2).alignment = align2
                # There may be video inserted (video is inserted directly as a file)
                try:
                    img = Image(img_path_real)
                     = 50
                     = 50
                    # Insert picture
                    sheet.add_image(img, position)
                except Exception as e:
                    (row=img_site1_2 + 1, column=img_site2_2).value = 'Video' + str(img_site1_2) + '_' + str(img_site2_2 - 11)
                    (row=img_site1_2 + 1, column=img_site2_2).alignment = Alignment(horizontal='center', vertical='center')
                # Progress bars
                (1)
                pass
        path_filename2 = savepath + '\\' + 'xxx year xx month_' + '.xlsx'
        (path_filename2)
    except Exception as e:
        print(e)
        return e

openpyxl hyperlink add

The openpyxl cell has the attribute hyperlink attribute

This attribute can set hyperlinks, if you just want to set a column with hyperlinks, you can use if to set the

def write_to_execl_link(filename = '. /new.xlsx',title='sheet',sheet_header = [],sheet_data = []):
    import openpyxl
    wb = ()
    ws = 
     = title
    row0 = sheet_header
    for i, r in enumerate(row0):
        (row=1, column=i + 1, value=r)
    for i, r in enumerate(sheet_data):
        for j, c in enumerate(r):
            # if i%2 == 1:
            #     (row=i + 2, column=j + 1).fill = fill
            (row=i + 2, column=j + 1, value=c)
            (row=i + 2, column=j + 1).hyperlink = r[-1]
 
    (filename)

openpyxl to get hyperlinks to file contents

import openpyxl
import os
# Put Excel files in python sibling directory
dir_path  = ((__file__))
test_xlsx = (dir_path,f'''''')
wb = openpyxl.load_workbook(test_xlsx)
sheet = 
print((1, 1).value)
print((1, 1).)

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.