0

I am creating a file which contain text data on 1st 4 pages and all images from page 5 onwards.

There is a table having page numbers as column. I want to add link to each of the page number in that column by clicking on which it should take me the the image page referenced by that page number.

I am creating this document using python-docx.

While stumbling on google I got a solution for creating hyperlink using python-docx. Clicking on the text with hyperlink takes me to url referenced by it.

The code for hyperlink is as follows:

import docx
from docx.enum.dml import MSO_THEME_COLOR_INDEX

def add_hyperlink(paragraph, text, url):
    # This gets access to the document.xml.rels file and gets a new relation id value
    part = paragraph.part
    r_id = part.relate_to(url, docx.opc.constants.RELATIONSHIP_TYPE.HYPERLINK, is_external=True)

    # Create the w:hyperlink tag and add needed values
    hyperlink = docx.oxml.shared.OxmlElement('w:hyperlink')
    hyperlink.set(docx.oxml.shared.qn('r:id'), r_id, )

    # Create a w:r element and a new w:rPr element
    new_run = docx.oxml.shared.OxmlElement('w:r')
    rPr = docx.oxml.shared.OxmlElement('w:rPr')

    # Join all the xml elements together add add the required text to the w:r element
    new_run.append(rPr)
    new_run.text = text
    hyperlink.append(new_run)

    # Create a new Run object and add the hyperlink into it
    r = paragraph.add_run ()
    r._r.append (hyperlink)

    # A workaround for the lack of a hyperlink style (doesn't go purple after using the link)
    # Delete this if using a template that has the hyperlink style in it
    r.font.color.theme_color = MSO_THEME_COLOR_INDEX.HYPERLINK
    r.font.underline = True

    return hyperlink


document = docx.Document()
p = document.add_paragraph('A plain paragraph having some ')
add_hyperlink(p, 'Link to Google', "http://google.com")
document.save('demo_hyperlink.docx')

I want that link to point to an inside document page.

P.Natu
  • 131
  • 1
  • 3
  • 12
  • Research the Word concept `Bookmark`. Bookmarks are the way Word "targets" links. Then you have two choices: `REF` fields or `Hyperlink` fields. The first is what Word uses when creating cross-references and has the option (field switch `\h` to hyperlink to the content; the second is a "standard" hyperlink, you can use the Insert/Hyperlink command to create such a hyperlink then inspect how Word does the targeting. – Cindy Meister May 30 '19 at 18:57
  • 3
    Does this answer your question? [how to create bookmarks in a word document, then create internal hyperlinks to the bookmark w/ python](https://stackoverflow.com/questions/57586400/how-to-create-bookmarks-in-a-word-document-then-create-internal-hyperlinks-to-t) – Martin Gergov Apr 24 '21 at 14:28

1 Answers1

0

i had the exact same question:

I got the answer from here hyperlink bookmarks

you can find an example of implementation here hyperlink to bookmark

Enjoy T

timmydodger
  • 81
  • 2
  • 8
  • Instead of linking to other answers on this site, you can either mark this question as a duplicate, or you can [edit] your answer with the code from the other answers, if you feel like this question has enough merit to be separate from the others – MindSwipe Jan 17 '20 at 11:40