使用 Python 在 Excel 中插入指向本地文件夹的超链接

Insert hyperlink to a local folder in Excel with Python

这段代码读取了一个Excel文件。此 excel 文件包含客户工作编号、客户姓名、站点、作品描述等信息。

此代码完成后(我希望)将执行的操作是读取工作表的最后一行(这是从工作表上单元格 'P1' 的计数器中获取的),根据单元格内容创建文件夹,并在工作表上创建超链接以打开已创建的最低本地文件夹。

我已经从工作表中提取了我需要的信息以了解需要创建哪些文件夹,但是我无法将超链接写入到 B 列中行上的单元格。

#Insert Hyperlink to folder
def folder_hyperlink(last_row_position, destination):
    cols = 'B'
    rows = str(last_row_position)
    position = cols + rows
    final_position = "".join(position)
    print final_position # This is just to check the value
    # The statement below should insert hyperlink in eps.xlsm > worksheet jobnoeps at column B and last completed row.
    ws.cell(final_position).hyperlink = destination

完整的代码在下面,但这里是用于创建超链接的部分。我也试过 'xlswriter' 包,但并不高兴。在互联网上搜索,上面的片段是我找到的结果。

有人知道我做错了什么吗?

 __author__ = 'Paul'

import os
import openpyxl
from openpyxl import load_workbook
import xlsxwriter

site_info_root = 'C:\Users\paul.EPSCONSTRUCTION\PycharmProjects\Excel_Jobs\Site Information\'

# This function returns the last row on eps.xlsm to be populated
def get_last_row(cell_ref = 'P1'): #P1 contains the count of the used rows
    global wb
    global ws
    wb = load_workbook("eps.xlsm", data_only = True) #Workbook
    ws = wb["jobnoeps"] #Worksheet
    last_row = ws.cell(cell_ref).value #Value of P1 from that worksheet
    return last_row


# This function will read the job number in format EPS-XXXX-YR
def read_last_row_jobno(last_row_position):
    last_row_data = []
    for cols in range(1, 5):
        last_row_data += str(ws.cell(column = cols, row = last_row_position).value)
    last_row_data_all = "".join(last_row_data)
    return last_row_data_all


#This function will return the Customer
def read_last_row_cust(last_row_position):
    cols = 5
    customer_name = str(ws.cell(column = cols, row = last_row_position).value)
    return customer_name


#This function will return the Site
def read_last_row_site(last_row_position):
    cols = 6
    site_name = str(ws.cell(column = cols, row = last_row_position).value)
    return site_name


#This function will return the Job Discription
def read_last_row_disc(last_row_position):
    cols = 7
    site_disc = str(ws.cell(column = cols, row = last_row_position).value)
    return site_disc


last_row = get_last_row()
job_no_details = read_last_row_jobno(last_row)
job_customer = read_last_row_cust(last_row)
job_site = read_last_row_site(last_row)
job_disc = read_last_row_disc(last_row)

cust_folder = job_customer
job_dir = job_no_details + "\" + job_site + " - " + job_disc


#Insert Hyperlink to folder
def folder_hyperlink(last_row_position, destination):
    cols = 'B'
    rows = str(last_row_position)
    position = cols + rows
    final_position = "".join(position)
    print final_position # This is just to check the value
    # The statement below should insert hyperlink in eps.xlsm > worksheet jobnoeps at column B and last completed row.
    ws.cell(final_position).hyperlink = destination



folder_location = site_info_root + job_customer + "\" + job_dir


print folder_location # This is just to check the value
folder_hyperlink(last_row, folder_location)

按照建议尝试 xlsxwriter 后,现在我的超链接函数看起来像这样。

##Insert Hyperlink to folder
def folder_hyperlink(last_row_position, destination):
    import xlsxwriter
    cols = 'B'
    rows = str(last_row_position)
    position = cols + rows
    final_position = "".join(position)
    print final_position # This is just to check the value
    workbook = xlsxwriter.Workbook('eps.xlsx')
    worksheet = workbook.add_worksheet('jobnoeps')
    print worksheet
    worksheet.write_url(final_position, 'folder_location')
    workbook.close()

该函数覆盖现有的 eps.xlsx,创建一个 jobnoeps table,然后插入超链接。我玩过以下几行,但不知道如何打开现有的 xlsx 和现有的 jobnoeps 选项卡,然后输入超链接。

workbook = xlsxwriter.Workbook('eps.xlsx')
worksheet = workbook.add_worksheet('jobnoeps')
worksheet.write_url(final_position, 'folder_location')

XlsxWriter write_url() 方法允许您 link 到文件夹或其他工作簿和工作表以及内部 link 和 link 到网址。例如:

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()

有关详细信息,请参阅上面 linked 的文档。

这里是成功的代码:-

# 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()

按照建议使用 openpyxl 再次尝试。