Issue
I am trying to copy contents from a source excel file to destination excel file. So my source excel file has hyperlinks in it. But when I paste the source file contents to destination excel file the hyperlinks are gone.
Is there any way to preserve the hyperlinks???
CODE:
import openpyxl as xl;
# opening the source excel file
filename ="C:\\Users\\Admin\\Desktop\\trading.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]
# opening the destination excel file
filename1 ="C:\\Users\\Admin\\Desktop\\test.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.active
# calculate total number of rows and
# columns in source excel file
mr = ws1.max_row
mc = ws1.max_column
# copying the cell values from source
# excel file to destination excel file
for i in range (1, mr + 1):
for j in range (1, mc + 1):
# reading cell value from source excel file
c = ws1.cell(row = i, column = j)
# writing the read value to destination excel file
ws2.cell(row = i, column = j).value = c.value
# saving the destination excel file
wb2.save(str(filename1))
Solution
Add ws2.cell(row = i, column = j).hyperlink = c.hyperlink
after ws2.cell(row = i, column = j).value = c.value
.
Do the same thing with .style
if you want to preserve the blue colour of the links.
Answered By - Duc Nguyen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.