使用Python从Excel(.xlsx)中提取超链接

Luc*_*asS 9 python xlrd hyperlink openpyxl

我一直在寻找用于Excel文件操作的xlrd和openpyxl库.但是,xlrd目前不支持formatting_info=True.xlsx文件,因此我无法使用xlrd hyperlink_map函数.所以我转向openpyxl,但也没有运气从excel文件中提取超链接.测试代码如下(测试文件包含一个简单的超链接,谷歌的超链接文本设置为"测试"):

import openpyxl

wb = openpyxl.load_workbook('testFile.xlsx')

ws = wb.get_sheet_by_name('Sheet1')

r = 0
c = 0

print ws.cell(row = r, column = c). value
print ws.cell(row = r, column = c). hyperlink
print ws.cell(row = r, column = c). hyperlink_rel_id
Run Code Online (Sandbox Code Playgroud)

输出:

test

None
Run Code Online (Sandbox Code Playgroud)

我想openpyxl目前还不支持完全格式化吗?是否有一些其他库可用于从Excel(.xlsx)文件中提取超链接信息?

wor*_*ise 6

现在应该可以通过openpyxl实现:

import openpyxl

wb = openpyxl.load_workbook('yourfile.xlsm')
ws = wb['Sheet1']
print(ws.cell(row=2, column=1).hyperlink.target)  # This will fail if there is no
                                                  # hyperlink to target
Run Code Online (Sandbox Code Playgroud)


小智 1

根据我的经验,获得良好的 .xlsx 交互需要迁移到 IronPython。这使您可以使用公共语言运行时 (clr) 并直接与 excel 交互”

http://ironpython.net/

import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()

wb = excel.Workbooks.Open('testFile.xlsx')
ws = wb.Worksheets['Sheet1']

address = ws.Cells(row, col).Hyperlinks.Item(1).Address
Run Code Online (Sandbox Code Playgroud)