简体   繁体   English

使用Python将超链接插入到Excel中的本地文件夹

[英]Insert hyperlink to a local folder in Excel with Python

The piece of code reads an Excel file. 该段代码读取一个Excel文件。 This excel file holds information such as customer job numbers, customer names, sites, works description ect.. 此excel文件包含诸如客户职位编号,客户名称,站点,作品说明等信息。

What this code will do when completed (I hope) is read the last line of the worksheet (this is taken from a counter on the worksheet at cell 'P1'), create folders based on cell content, and create a hyperlink on the worksheet to open the lowest local folder that was created. 这段代码完成后会做什么(我希望)是读取工作表的最后一行(这是从工作表上“ P1”单元格的计数器中提取的),根据单元格内容创建文件夹,并在工作表上创建超链接打开创建的最低本地文件夹。

I have extracted the info I need from the worksheet to understand what folders need to be created, but I am not able to write a hyperlink to the cell on the row in column B. 我已经从工作表中提取了我需要的信息,以了解需要创建哪些文件夹,但是我无法在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

The complete code is below but here is the section that is meant to create the hyperlink. 完整的代码在下面,但此处是用于创建超链接的部分。 I have also tried the 'xlswriter' package with no joy. 我也没有高兴地尝试过“ xlswriter”软件包。 Searched the internet and the above snippet is the result of what I found. 搜索了互联网,上面的代码段是我发现的结果。

Anyone know what I am doing wrong? 有人知道我在做什么错吗?

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

Now my hyperlink function looks like this after trying xlsxwriter as advised. 现在,按照建议尝试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()

The function overwrites the exsisting eps.xlsx, creates a jobnoeps table and then inserts the hyperlink. 该函数将覆盖现有的eps.xlsx,创建一个作业表,然后插入超链接。 I have played with the following lines but don't know how to get it to open the existing xlsx and existing jobnoeps tab and then enter the hyperlink. 我玩过以下几行,但不知道如何打开现有的xlsx和现有的jobnoeps选项卡,然后输入超链接。

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

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. XlsxWriter write_url()方法允许您链接到文件夹或其他工作簿和工作表,以及内部链接和Web url的链接。 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. 按照建议再次与openpyxl一起尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM