我正在尝试使用python将超链接添加到Word文档。但是,当我这样做时,代码无法识别要超链接的所有文本。有时,它有时会超链接。没有关于文档中哪些单词超链接的模式,它只是随机地超链接。例如,A,B和C可能是单词文档中需要超链接的文本,但是代码第一次仅标识A,然后第二次标识B和A,并且没有出现任何模式。下面是我的代码。
import win32com.client
import re
import os
class HyperlinkDocument:
#This method creates a dictionary that will store the file name with its corresponding path
def pdfLists(self,fileLocations):
#create an empty dictionary that will later be populated.
fileDictionary={}
for root, dirs, files in os.walk(fileLocations):
for file in files:
if file.endswith(".pdf") or file.endswith(".PDF"):
#the line below will remove the .pdf extension from the file name
fileName=str(file)[:-4]
#add the key(file name) and its corresponding value (file path) to the dictionary
fileDictionary.update({fileName:os.path.join(root, file)})
return fileDictionary
#This method will open and hyperlink the word document
def hyperlinkMethod(self,fileName,filePath,wordFileName):
# Open microsoft word
wordapp = win32com.client.Dispatch("Word.Application")
# Open the input file where you want to insert the hyperlinks:
#users input is based on what the user entered as the file they want to edit
directoryPath = os.path.dirname(os.path.realpath("fileProject.py"))
fullPath=directoryPath+"\\"+wordFileName
wordapp.Documents.Open(fullPath)
# Select the currently active document
doc = wordapp.ActiveDocument
# First capture the entire document's content as text
docText = doc.Content.text
# Search for text that matches the file names identified:
textToHyperlink = re.findall(fileName, 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(textToHyperlink)):
current_string_to_link = str(textToHyperlink[linkIndex])
link_address = filePath
#Issue identified with the line below.
if wordapp.Selection.Find.Execute(FindText=current_string_to_link)==True:
doc.Hyperlinks.Add(Anchor=wordapp.Selection.Range,
Address=link_address)
else:
print(current_string_to_link)
if __name__ == '__main__':
print("Hey, enjoy your hyperlinking!")
# error handling to avoid empty strings
while True:
fileLocations=input("Enter the name of the folder with the pdfs:"+"\n")
if not fileLocations:
print('No locations specified')
continue
else:
break
while True:
wordFileName = input("Enter the name of the word document with its location:"+
'\n'+"For example: HyperlinkProject/test.docx >>"+"\n")
if not wordFileName:
print('No word document specified')
continue
else:
break
hyperlinkDocument = HyperlinkDocument()
fileDictionary = hyperlinkDocument.pdfLists(fileLocations)
# pass the dictionary through the hyperlink method
try:
for y in fileDictionary:
hyperlinkDocument.hyperlinkMethod(y, fileDictionary.get(y), wordFileName)
print("Done!")
except Exception as e:
print("An error occurred: " + e)