【问题标题】:Adding hyperlinks in Microsoft word using python使用 python 在 Microsoft Word 中添加超链接
【发布时间】:2016-01-06 15:09:44
【问题描述】:

过去几个小时我一直在搜索,但找不到允许我使用 python 将超链接添加到 word 文档的库。在我的理想世界中,我将能够使用 python 来操作 word doc,以将超链接添加到链接到内部文档的脚注。 Python-docx 好像没有这个功能。

它分为 2 个问题。 1)有没有办法使用python向word docs添加超链接? 2) 有没有办法使用 python 来操作 word 文档中的脚注?

有谁知道如何做到这一点或其中的任何部分?

【问题讨论】:

  • 您可以使用 python-docx 从(正确的)xml 创建 docx 文档。如果您创建带有超链接的 word 文档,您可以将其解压缩,查看正确的标记是什么,并将其用作模板来操作现有文档。
  • 在 Marcin 的评论之上,如果您查看并挖掘 here,它可能会有所帮助。不确定这是否曾经被合并到主服务器中。

标签: python hyperlink ms-word docx


【解决方案1】:

可以使用 win32com 包添加超链接:

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

【讨论】:

    【解决方案2】:
    # 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')
    

    【讨论】:

      猜你喜欢
      • 2021-06-25
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      相关资源
      最近更新 更多