Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding hyperlinks in Microsoft word using python

I've been searching for the last few hours and cannot find a library that allows me to add hyperlinks to a word document using python. In my ideal world I'd be able to manipulate a word doc using python to add hyperlinks to footnotes which link to internal documents. Python-docx doesn't seem to have this feature.

It breaks down into 2 questions. 1) Is there a way to add hyperlinks to word docs using python? 2) Is there a way to manipulate footnotes in word docs using python?

Does anyone know how to do this or any part of this?

like image 568
user3368835 Avatar asked Dec 28 '25 18:12

user3368835


2 Answers

Hyperlinks can be added using the win32com package:

import win32com.client

#connect to Word (start it if it isn't already running)
wordapp = win32com.client.Dispatch("Word.Application")

#add a new document
doc = wordapp.Documents.Add()

#add some text and turn it into a hyperlink
para = doc.Paragraphs.Add()
para.Range.Text = "Adding hyperlinks in Microsoft word using python"
doc.Hyperlinks.Add(Anchor=para.Range,  Address="http://stackoverflow.com/questions/34636391/adding-hyperlinks-in-microsoft-word-using-python")
#In theory you should be able to also pass in a TextToDisplay argument to the above call but I haven't been able to get this to work
#The workaround is to insert the link text into the document first and then convert it into a hyperlink
like image 115
bard Avatar answered Dec 31 '25 06:12

bard


# How to insert hyperlinks into an existing MS Word document using win32com:

# Use the same call as in the example above to connect to Word:
wordapp = win32com.client.Dispatch("Word.Application")

# Open the input file where you want to insert the hyperlinks:
wordapp.Documents.Open("my_input_file.docx")

# Select the currently active document
doc = wordapp.ActiveDocument

# For my application, I want to replace references to identifiers in another 
# document with the general format of "MSS-XXXX", where X is any digit, with 
# hyperlinks to local html pages that capture the supporting details...

# First capture the entire document's content as text
docText = doc.Content.text

# Search for all identifiers that match the format criteria in the document:
mss_ids_to_link = re.findall('MSS-[0-9]+', docText)

# Now loop over all the identifier strings that were found, construct the link
# address for each html page to be linked, select the desired text where I want
# to insert the hyperlink, and then apply the link to the correct range of 
# characters:
for linkIndex in range(len(mss_ids_to_link)):
    current_string_to_link = mss_ids_to_link[linkIndex]
    link_address           = html_file_pathname + \
                             current_string_to_link + '.htm'
    if wordapp.Selection.Find.Execute(FindText=current_string_to_link, \
                                      Address=link_address) == True:
        doc.Hyperlinks.Add(Anchor=wordapp.Selection.Range, \
                           Address=link_address)

# Save off the result:
doc.SaveAs('my_input_file.docx')
like image 25
James McComb Avatar answered Dec 31 '25 07:12

James McComb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!