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

这段代码读取一个Excel文件。 这个excel文件保存的信息,如客户的工作号码,客户名称,网站,作品说明等。

什么代码将完成时(我希望)读取工作表的最后一行(这是从单元格'P1'工作表上的计数器),创build基于单元格内容的文件夹,并在工作表上创build一个超链接打开创build的最低本地文件夹。

我已经从工作表中提取了我需要的信息,以了解需要创build哪些文件夹,但是我无法在列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 

完整的代码如下,但是这里是创build超链接的部分。 我也尝试过“xlswriter”包,没有任何的喜悦。 search了互联网和上面的片段是我发现的结果。

任何人都知道我在做什么错了?

  __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之后build议。

 ##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,创build一个jobnoeps表,然后插入超链接。 我已经玩了以下几行,但不知道如何打开现有的xlsx和现有的jobnoeps选项卡,然后input超链接。

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

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

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

有关更多详细信息,请参阅上面链接到的文档。

这里是诀窍的代码: –

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

build议再次尝试使用openpyxl。