How to copy hyperlinks using xlrd, xlwt and xlutils?

S

Sameer Gupta

Hello Everyone!

*Problem:*
I am trying a simple code for writting excel sheet. The program checks if
the Excel sheet already exists, If the file exists, then it append it with
the new data. The problem is I am unable to copy hyperlinks. I would be very
thankful if anyone can suggest me some way.

*Note:* I had used here some default example for hyperlinks. I will be using
this information for my other program where I am suppose to edit excel sheet
with many sheets and every sheet containing hyperlinks at multiple places.

*Code: *
from xlwt import *
import xlrd as xr
import os
from xlutils.copy import copy

name=r"hyperlinks.xls"
if os.path.exists(name)==True:
print "Excel sheet already exists!!!"
cwb=xr.open_workbook(name,formatting_info=True)
w=copy(cwb)
temp=cwb.sheet_by_index(0)
ws=w.get_sheet(0)
row=len(temp.col_values(0))
f = Font()
f.height = 20*72
f.name = 'Verdana'
f.bold = True
f.underline = Font.UNDERLINE_DOUBLE
f.colour_index = 4
h_style = XFStyle()
h_style.font = f
n = "HYPERLINK"
ws.write_merge(row+1, row+1,1, 10, Formula(n +
'("http://www.irs.gov/pub/irs-pdf/f1000.pdf";"f1000.pdf")'), h_style)
ws.write_merge(row+2, row+2, 2, 25, Formula(n +
'("mailto:[email protected]?subject=pyExcelerator-feedback&Body=Hello,%20Roman!";"pyExcelerator-feedback")'),
h_style)
w.save("hyperlinks.xls")
else:
f = Font()
f.height = 20*72
f.name = 'Verdana'
f.bold = True
f.underline = Font.UNDERLINE_DOUBLE
f.colour_index = 4

h_style = XFStyle()
h_style.font = f

w = Workbook()
ws = w.add_sheet('F')
n = "HYPERLINK"
ws.write_merge(1, 1, 1, 10, Formula(n +
'("http://www.irs.gov/pub/irs-pdf/f1000.pdf";"f1000.pdf")'), h_style)
ws.write_merge(2, 2, 2, 25, Formula(n +
'("mailto:[email protected]?subject=pyExcelerator-feedback&Body=Hello,%20Roman!";"pyExcelerator-feedback")'),
h_style)

w.save("hyperlinks.xls")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top