powershell 如何将RTF文档转换为docx

bxjv4tth  于 2023-01-26  发布在  Shell
关注(0)|答案(1)|浏览(126)

我在here上发现了类似的东西,但是当我尝试运行这个时,我得到了错误。因此,我想知道是否有可能制作一个Powershell脚本,可以接受.RTF文档并将它们全部转换为.docx文档?

cwxwcias

cwxwcias1#

使用此命令将rtf转换为docx:

Function Convert-Dir($path){
    $Files=Get-ChildItem "$($path)\*.docx" -Recurse
    $Word=New-Object –ComObject WORD.APPLICATION

    foreach ($File in $Files) {
        # open a Word document, filename from the directory
        $Doc=$Word.Documents.Open($File.fullname)

        # Swap out DOCX with PDF in the Filename
        $Name=($Doc.Fullname).replace("docx","doc")

        if (Test-Path $Name){

        } else {
            # Save this File as a PDF in Word 2010/2013
            Write-Host $Name
            $Doc.saveas([ref] $Name, [ref] 0)  
            $Doc.close()
        }
    }

    $Files=Get-ChildItem "$($path)\*.rtf" -Recurse
    $Word=New-Object –ComObject WORD.APPLICATION

    foreach ($File in $Files) {
        # open a Word document, filename from the directory
        $Doc=$Word.Documents.Open($File.fullname)

        # Swap out DOCX with PDF in the Filename
        $Name=($Doc.Fullname).replace("rtf","doc")

        if (Test-Path $Name){

        } else {
            # Save this File as a PDF in Word 2010/2013
            Write-Host $Name
            $Doc.saveas([ref] $Name, [ref] 0)  
            $Doc.close()
        }
    }
}

Convert-Dir "RtfFilePath";

代码来源和属性:https://gist.github.com/rensatsu/0a66a65c3a508ecfd491#file-rtfdocxtodoc-ps1

相关问题