2016-01-06 1451 views
3

我一直在搜索最近幾個小時,並且找不到一個允許使用python將超鏈接添加到word文檔的庫。在我的理想世界中,我可以使用python來操作word doc以將超鏈接添加到鏈接到內部文檔的腳註。 Python-docx似乎沒有這個功能。使用python在Microsoft word中添加超鏈接

它分爲2個問題。 1)有沒有辦法使用python添加超文本鏈接到word文檔? 2)有沒有辦法使用python操作word docs中的腳註?

有誰知道如何做到這一點或者這個的任何部分?

+0

您可以使用Python,DOCX創建一個從(正確的)XML文檔DOCX。如果您使用超鏈接創建Word文檔,可以將其解壓縮,查看正確的標記,並將其用作模板來操作現有文檔。 – Marcin

+0

在Marcin的評論之上,如果你看看並且在這裏挖掘(https://github.com/python-openxml/python-docx/issues/74),它可能會有所幫助。不知道這是否合併到主。 – disflux

回答

2

超鏈接可以使用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