查找和更改 word 中的超链接的 Powershell 脚本保存文档并创建新副本为 PDF

Dav*_*ton 2 powershell

我想弄清楚如何编写一个脚本,该脚本通过一个文件夹并抓取文件夹中的所有 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()
Run Code Online (Sandbox Code Playgroud)

我是 powershell 的新手,请有人帮助我完成这些步骤。我听说 powershell 可能是完成此类事情的最佳和最强大的语言。

mro*_*off 5

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

$NewDomain1 = "google"
$NewDomain2 = "hij"
$OurDocuments = Get-ChildItem -Path "C:\Apps\testing" -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()
Run Code Online (Sandbox Code Playgroud)

让我们走过它...

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

$NewAddress1 = "https://www.google.com/"
$NewAddress2 = "http://hij.com/"
$OurDocuments = Get-ChildItem -Path "C:\Apps\testing" -Filter "*.doc*" -Recurse
Run Code Online (Sandbox Code Playgroud)

实例化我们的 Word Com 对象并将其隐藏起来。

$Word = New-Object -ComObject word.application
$Word.Visible = $false
Run Code Online (Sandbox Code Playgroud)

进入我们的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
    }
}
Run Code Online (Sandbox Code Playgroud)

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

"Saving changes to {0}" -f $Document.Fullname
$Document.Save()    
Run Code Online (Sandbox Code Playgroud)

在这里,我们为另存为 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)
Run Code Online (Sandbox Code Playgroud)

关闭文档,循环将移动到$OurDocuments.

"Completed processing {0} `r`n" -f $Document.Fullname
$Document.Close()
Run Code Online (Sandbox Code Playgroud)

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

$Word.Quit()
Run Code Online (Sandbox Code Playgroud)

我希望一切都有意义!