1

I have a python script that creates a docx file using python-docx. The document file has hyperlinks set to the bookmarks linked inside the document paragraphs. It shows correctly if I open the document within my laptop. But when I open the document on google drive after uploading, it is skipping the hyperlink part. Any ideas what might be going wrong? This is how I am uploading the document using pydrive. Should I try to upload it in PDF format if that helps?

    http = drive.auth.Get_Http_Object()
    # createfile 
    file1 = drive.CreateFile({'title': 'Test Report.docx', 
        'mimeType': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'})            
    file1.SetContentFile("read.docx")
    file1.Upload(param={"http": http})
rain
  • 363
  • 6
  • 15
  • What code are you using to create the hyperlinks? – scanny Apr 06 '21 at 20:04
  • There is a thread on this: https://stackoverflow.com/questions/57586400/how-to-create-bookmarks-in-a-word-document-then-create-internal-hyperlinks-to-t – rain Apr 06 '21 at 20:11

1 Answers1

1

Found the right way to do this without having to open the word document. It needs PyWin32 lib to be installed.

import win32com.client
def update_toc(file):
    word = win32com.client.DispatchEx("Word.Application")
    print(os.path.join(os.getcwd(), file))
    doc = word.Documents.Open(os.path.join(os.getcwd(), file))
    doc.TablesOfContents(1).Update()
    word.ActiveDocument.Fields.Update()   
    doc.Close(SaveChanges=True)
    word.Application.Quit()
rain
  • 363
  • 6
  • 15