Skip to main content
 首页 » 编程设计

python之如何将本 map 片的超链接写入openpyxl中的单元格

2025年05月04日23exmyth

我使用 Python 2.7.3 我需要通过 openpyxl 库将指向本 map 片的超链接写入单元格。

当我需要添加到网站的超链接时,我会这样写:

从 openpyxl 导入工作簿

wb = Workbook() 
 
dest_filename = r'empty_book.xlsx' 
 
ws = wb.worksheets[0] 
 
ws.title = 'Name' 
 
hyperlink to local picture 
 
ws.cell('B1').hyperlink = ('http://pythonhosted.org/openpyxl/api.html') 
 
hyperlink to local picture 
 
ws.cell('B2').hyperlink = ('1.png') # It doesn't work!  
 
wb.save(filename = dest_filename) 

我有 3 个问题:

  1. 我们如何像 VBA 的样式函数一样编写超链接: ActiveCell.FormulaR1C1 = _ "=HYPERLINK(""http://stackoverflow.com/questions/ask"",""site"")" 带有超链接和她的名字
  2. 我们如何编写指向本地镜像的超链接? ws.cell('B2').hyperlink = ('1.png') # It doesn't work! And I don't now what to do ) Plese, help me )
  3. 我们可以使用 unicode 超链接到图像吗?例如当我使用 ws.cell('B1').hyperlink = (u'http://pythonhosted.org/openpyxl/api.html') It fail with error! for example we have picture 'russian_language_name.png' and we create hyperlink in exel without any problem. We click to the cell, and then print '=Hyperlink("http://stackoverflow.com/questions/ask";"site_by_russian_language")

保存文件,解压他。然后我们去他的目录到xl->worksheets->sheet1.xml 我们看到了标题

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

然后……

row r="2" x14ac:dyDescent="0.25" spans="2:6">-<c r="B2" t="str" s="1"><f>HYPERLINK("http://stackoverflow.com/questions/ask","site_by_russian_language")</f><v>site_by_russian_language</v></c>

一切正常 =) Exel 支持 unicode,但是 python 的库 openpyxl 呢?它支持超链接中的 unicode ?

请您参考如下方法:

由于 .xlsx 文件中的文件是采用 UTF-8 编码的 XML 文件,因此 Unicode 超链接不是问题。