0

I run ubuntu 19, I tried some examples from #74 Github and Adding an hyperlink in MSWord by using python-docx, the hyperlink doesn;t seem to work in libreoffice or even Google Docs, is there a way of making it work on Google Docs and LibreOffice ?

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 site', "https://www.google.com")
p.add_run('hello this is after link')
document.save('demo_hyperlink.docx')
  • Does it work on Word, just not on the others? – scanny Mar 31 '20 at 18:12
  • @scanny I am not sure if it works on Word (Office), we don't run windows here –  Mar 31 '20 at 20:59
  • In order to manage the Google Document, it is required to use [Google Docs API](https://developers.google.com/docs/api). Unfortunately, python-docx cannot manage the Google Document. So how about using googleapis for python? You can see the sample script at [the official document](https://developers.google.com/sheets/api/quickstart/python). If this was not the direction you want, I apologize. – Tanaike Mar 31 '20 at 23:35
  • @Tanaike this works great, you can post as an answer if you want –  Apr 01 '20 at 05:31
  • Thank you for replying. I noticed that the link of sample script for using Docs API with googleapis of python is not correct. So I modified it and posted it as an answer. Could you please confirm it? – Tanaike Apr 01 '20 at 05:41

1 Answers1

1

In order to manage the Google Document, it is required to use Google Docs API.

Unfortunately, python-docx cannot manage the Google Document. Because it cannot use the Docs API. So here, I would like to propose to use the googleapis for python.

You can see the sample script at the official document.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Well, I ran the code but I can't find the document (they created) in Google docs or Google Drive –  Apr 01 '20 at 06:30
  • @User1984 From your previous replying, you said `this works great`. But from your comment, you say `I ran the code but I can't find the document (they created) in Google docs or Google Drive`. I have to apologize for my poor English skill. From your replying, I cannot understand about your current situation. Can I ask you about the detail of your current situation? – Tanaike Apr 01 '20 at 06:39
  • It works, the Initial code [https://developers.google.com/docs/api/quickstart/python] will only create a connection, this [https://developers.google.com/docs/api/how-tos/documents] will actually create documents –  Apr 01 '20 at 06:45
  • @User1984 Thank you for replying. From your replying, I could understand that you use the authorization script from the quickstart and use the create method from the sample of "Creating a blank document". As an important point, when you use the access token retrieved by OAuth2, you can see the created Document at your Google Drive. When you use the service account, you cannot directly see the created Document at your Google Drive. Please be careful this. – Tanaike Apr 01 '20 at 06:49