简体   繁体   English

openpyxl 中超链接的奇怪行为

[英]Weird behaviour of hyperlinks in openpyxl

I have two columns of data in Excel and am trying to use openpyxl to format them.我在 Excel 中有两列数据,我正在尝试使用 openpyxl 来格式化它们。

The first column is a URL.第一列是一个 URL。 I want to set the second column to be a hyperlink to the URL and then delete the first column.我想将第二列设置为 URL 的超链接,然后删除第一列。 I am also setting the formatting of this column to look like a hyperlink - Underlined and blue color.我还将此列的格式设置为看起来像一个超链接 - 下划线和蓝色。 I am using the following code.我正在使用以下代码。 (The below code only has one line of data so that it is easier to understand) (以下代码只有一行数据,便于理解)

import openpyxl
from openpyxl import Workbook
from openpyxl.styles import Font

#Set Up Workbook
wb = Workbook()
ws = wb.active
ws['A1'] = r'C:\Users'
ws['B1'] = 'Click To Open Users'
ws['C1'] = 'qwertyuiop'

#Format Cells
ws.cell(row=1,column=2).hyperlink = ws.cell(row=1,column=1).value
ws.cell(row=1,column=2).font = Font(color="0000EE", underline='single')

#Delete First column (The other columns should shift to the left)
ws.delete_cols(1)  


wb.save(r"C:\Test.xlsx")

However the result of this code is an excel sheet with a hyperlink in the 2nd column!然而,这段代码的结果是一个 Excel 表,在第二列中有一个超链接! (The blue format and underline are in the 1st column as expected) (蓝色格式和下划线按预期在第一列中)

When I don't delete the 1st column everything works as expected so this is just a weird quirk that when I delete the first column the hyperlink doesn't move over.当我不删除第一列时,一切都按预期工作,所以这只是一个奇怪的怪癖,当我删除第一列时,超链接不会移动。

Does anyone know why this is happening?有谁知道为什么会这样?

I used version 2.6.1 and then upgraded to 3.0.3 and the same thing was happening.我使用了 2.6.1 版本,然后升级到 3.0.3 并且发生了同样的事情。

Developers do not consider it as an issue as "openpyxl is not a replacement for Excel so it does not keep track of stuff when you make changes such as adding or deleting rows or columns. You have to do this in your own code. We moved cell objects and they contain formatting but hyperlinks are distinct objects that refer to cells. We also generally don't update formulae or charts."开发人员不认为这是一个问题,因为“openpyxl 不是 Excel 的替代品,因此当您进行诸如添加或删除行或列之类的更改时,它不会跟踪内容。您必须在自己的代码中执行此操作。我们移动了单元格对象,它们包含格式,但超链接是引用单元格的不同对象。我们通常也不更新公式或图表。”

Link here: https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1429链接在这里: https : //foss.heptapod.net/openpyxl/openpyxl/-/issues/1429

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

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