如何在openpyxl中将本地图片的超链接写入单元格?

2024-03-28 17:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python2.7.3 我需要用openpyxl库将本地图片的超链接写入单元格。在

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

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

我有三个问题:

  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?在


Tags: tohttpwspngsitecellitfilename
3条回答

关于问题2,我想你需要包括文件链接的完整路径。 如果您无法访问Excel文件中的文件链接,则是Excel的安全策略禁止此类操作。在

我回答了一个类似的问题。希望这有帮助。在

好吧,我可以做到这一点。虽然没有直接的方法来建立一个超链接,在你的情况下,我们可以这样做。我能够建立一个超链接到一个现有的文件使用下面的代码。在

wb=openpyxl.Workbook() s = wb.get_sheet_by_name('Sheet') s['B4'].value = '=HYPERLINK("C:\\Users\\Manoj.Waghmare\\Desktop\\script.txt", "newfile")' s['B4'].style = 'Hyperlink' wb.save('trial.xlsx') 将样式属性称为“超链接”是关键。我所有的其他代码对你来说可能并不重要。否则样式属性会有一个值'Normal'奇怪的是即使没有样式属性,我们工作的超链接也只是缺少样式!当然。虽然很奇怪,但我看到了一些奇怪的东西。希望这有帮助。在

由于.xlsx文件中的文件是使用UTF-8编码的XML文件,所以Unicode超链接不是问题。在

相关问题 更多 >