简体   繁体   中英

Weird behaviour of hyperlinks in openpyxl

I have two columns of data in Excel and am trying to use openpyxl to format them.

The first column is a URL. I want to set the second column to be a hyperlink to the URL and then delete the first column. 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! (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.

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."

Link here: https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1429

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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