在 Word 中查找和更改超链接的 Powershell 脚本会保存文档并创建一个新副本作为 PDF



我正在尝试弄清楚如何编写一个脚本,该脚本通过文件夹并获取文件夹中的所有word文档以搜索超链接并将链接更改为另一个链接。然后保存该word文档并创建另一个版本将其转换为pdf。

如何调整下面的脚本以抓取文件夹中的所有Word文档,然后在所有超链接中搜索"https://www.yahoo.com"到"https://www.google.com"。一些如何遍历整个文档搜索所有超链接。保存该文档,然后转换并生成新的 pdf。

这可能吗?

到目前为止我有什么

$word = New-Object -ComObject word.application
$document = $word.documents.open("path to folder")
$hyperlinks = @($document.Hyperlinks) 
$hyperlinks | ForEach {
$newURI = ([uri]$_.address).AbsoluteUri
Write-Verbose ("Updating {0} to {1}" -f $_.Address,$newURI) -Verbose
$_.address = $newURI
}
If (_$.address -eq "https://www.yahoo.com/") {
$_.Address = "https://www.google.com/"
} ElseIf ($_.Address -eq "http://def.com/motorists") {
$_.Address = "http://hij.com/"
}
$document.save()
$word.quit()
Get-ChildItem -Path $document -Include *.doc, *.docx -Recurse |
ForEach-Object {
$doc = $word.Documents.Open($_.Fullname)
$pdf = $_.FullName -replace $_.Extension, '.pdf'
$doc.ExportAsFixedFormat($pdf,17)
$doc.Close()
}
$word.Quit()

我是Powershell的新手,请有人帮助引导我完成这些步骤。我听说powershell可能是完成这种事情的最好和最强大的语言。

以前没有这样做过,所以很高兴弄清楚。我们今天都要学习!你们离得很近。只需要一些调整和一个循环来处理多个文件。我相信知识渊博的人会加入,但这应该会给你带来想要的结果。

$NewDomain1 = "google"
$NewDomain2 = "hij"
$OurDocuments = Get-ChildItem -Path "C:Appstesting" -Filter "*.doc*" -Recurse
$Word = New-Object -ComObject word.application
$Word.Visible = $false
$OurDocuments | ForEach-Object {
$Document = $Word.documents.open($_.FullName)
"Processing file: {0}" -f $Document.FullName
$Document.Hyperlinks | ForEach-Object {
if ($_.Address -like "https://www.yahoo.com/*") {
$NewAddress = $_.Address -Replace "yahoo","google"
"Updating {0} to {1}" -f $_.Address,$NewAddress
$_.Address = $_.TextToDisplay = $NewAddress
} elseif ($_.Address -like "http://def.com/*") {
$NewAddress = $_.Address -Replace "def","hij"
"Updating {0} to {1}" -f $_.Address,$NewAddress
$_.Address = $_.TextToDisplay = $NewAddress
}
}
"Saving changes to {0}" -f $Document.Fullname
$Document.Save()    
$Pdf = $Document.FullName -replace $_.Extension, '.pdf'
"Saving document {0} as PDF {1}" -f $Document.Fullname,$Pdf
$Document.ExportAsFixedFormat($Pdf,17)
"Completed processing {0} `r`n" -f $Document.Fullname
$Document.Close()
}
$Word.Quit()

让我们走一走...

我们将首先将您的新地址移动到几个变量中,以便将来引用和更改。您还可以在此处添加要查找的地址,根据需要替换硬编码字符串。第三行使用筛选器来获取目录中的所有.DOC和.DOCX文件,我们将使用这些文件进行迭代。就个人而言,我会小心使用-Recurse开关,因为您冒着对目录结构中更深的文件进行意外更改的风险。

$NewAddress1 = "https://www.google.com/"
$NewAddress2 = "http://hij.com/"
$OurDocuments = Get-ChildItem -Path "C:Appstesting" -Filter "*.doc*" -Recurse

实例化我们的 Word Com 对象并将其隐藏在视图中。

$Word = New-Object -ComObject word.application
$Word.Visible = $false

走进我们的ForEach-Object循环...

对于我们在$OurDocuments中收集的每个文档,我们打开它并将任何超链接通过管道传输到另一个ForEach-Object,在那里我们检查Address属性的值。如果存在我们想要的匹配项,我们将使用新值更新属性。您会注意到,我们还在更新TextToDisplay属性。这是您在文档中看到的文本,而不是控制超链接实际去向Address

这。。。$_.Address = $_.TextToDisplay = $NewAddress1......是多变量赋值的一个示例。由于AddressTextToDisplay将设置为相同的值,因此我们将同时分配它们。

$Document = $Word.documents.open($_.FullName)
"Processing file: {0}" -f $Document.FullName
$Document.Hyperlinks | ForEach-Object {
if ($_.Address -like "https://www.yahoo.com/*") {
$NewAddress = $_.Address -Replace "yahoo","google"
"Updating {0} to {1}" -f $_.Address,$NewAddress
$_.Address = $_.TextToDisplay = $NewAddress
} elseif ($_.Address -like "http://def.com/*") {
$NewAddress = $_.Address -Replace "def","hij"
"Updating {0} to {1}" -f $_.Address,$NewAddress
$_.Address = $_.TextToDisplay = $NewAddress
}
}

保存所做的任何更改...

"Saving changes to {0}" -f $Document.Fullname
$Document.Save()    

在这里,我们为另存为 PDF 时创建新的文件名。请注意第一行中的$_.Extension。我们切换到使用管道对象来引用文件扩展名,因为当前管道对象仍然是我们Get-ChildItem中的文件信息对象。由于$Document对象没有扩展属性,因此必须对文件名进行一些切片才能获得相同的结果。

$Pdf = $Document.FullName -replace $_.Extension, '.pdf'
"Saving document {0} as PDF {1}" -f $Document.Fullname,$Pdf
$Document.ExportAsFixedFormat($Pdf,17)

关闭文档,循环将移动到$OurDocuments中的下一个文件。

"Completed processing {0} `r`n" -f $Document.Fullname
$Document.Close()

浏览所有文档后,我们将关闭 Word。

$Word.Quit()

我希望一切都有意义!

最新更新