首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基本Powershell -批量将Word Docx转换为PDF

基本Powershell -批量将Word Docx转换为PDF
EN

Stack Overflow用户
提问于 2013-05-14 10:44:24
回答 5查看 60.4K关注 0票数 33

我正在尝试使用PowerShell批量转换Word Docx到PDF -使用在这个网站上找到的脚本:http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/24/weekend-scripter-convert-word-documents-to-pdf-files-with-powershell.aspx

代码语言:javascript
复制
# Acquire a list of DOCX files in a folder
$Files=GET-CHILDITEM "C:\docx2pdf\*.DOCX"
$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","pdf")

    # Save this File as a PDF in Word 2010/2013
    $Doc.saveas([ref] $Name, [ref] 17)  
    $Doc.close()
}

我一直收到这个错误,但找不到原因:

代码语言:javascript
复制
PS C:\docx2pdf> .\docx2pdf.ps1
Exception calling "SaveAs" with "16" argument(s): "Command failed"
At C:\docx2pdf\docx2pdf.ps1:13 char:13
+     $Doc.saveas <<<< ([ref] $Name, [ref] 17)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

有什么想法吗?

另外,我需要如何将其更改为也转换文档(而不是docX)文件,以及使用本地文件(与脚本位置相同的文件)?

对不起-从来没有做过PowerShell脚本...

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-05-14 16:04:53

这将适用于doc和docx文件。

代码语言:javascript
复制
$documents_path = 'c:\doc2pdf'

$word_app = New-Object -ComObject Word.Application

# This filter will find .doc as well as .docx documents
Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object {

    $document = $word_app.Documents.Open($_.FullName)

    $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"

    $document.SaveAs([ref] $pdf_filename, [ref] 17)

    $document.Close()
}

$word_app.Quit()
票数 66
EN

Stack Overflow用户

发布于 2016-12-20 17:51:07

上面的答案对我来说都不是很好,因为我正在用这种方法批量转换大约70,000个word文档。事实证明,反复这样做最终会导致单词崩溃,可能是因为内存问题(错误是一些我不知道如何解析的COMException )。所以,为了让它继续运行,我的技巧是每100个文档(任意选择的数字)终止并重新启动word。

此外,当它偶尔崩溃时,会产生格式错误的pdf,每个pdf的大小通常为1-2kb。因此,当跳过已经生成的pdf时,我要确保它们的大小至少为3kb。如果你不想跳过已经生成的PDF,你可以删除If语句。

对不起,如果我的代码看起来不好,我通常不使用Windows,这是一个一次性的黑客攻击。所以,下面是结果代码:

代码语言:javascript
复制
$Files=Get-ChildItem -path '.\path\to\docs' -recurse -include "*.doc*"

$counter = 0
$filesProcessed = 0
$Word = New-Object -ComObject Word.Application

Foreach ($File in $Files) {
    $Name="$(($File.FullName).substring(0, $File.FullName.lastIndexOf("."))).pdf"
    if ((Test-Path $Name) -And (Get-Item $Name).length -gt 3kb) {
        echo "skipping $($Name), already exists"
        continue
    }

    echo "$($filesProcessed): processing $($File.FullName)"
    $Doc = $Word.Documents.Open($File.FullName)
    $Doc.SaveAs($Name, 17)
    $Doc.Close()
    if ($counter -gt 100) {
        $counter = 0
        $Word.Quit()
        [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
        $Word = New-Object -ComObject Word.Application
    }
    $counter = $counter + 1
    $filesProcessed = $filesProcessed + 1
}
票数 7
EN

Stack Overflow用户

发布于 2013-05-14 15:11:32

这对我很有效(Word 2007):

代码语言:javascript
复制
$wdFormatPDF = 17
$word = New-Object -ComObject Word.Application
$word.visible = $false

$folderpath = Split-Path -parent $MyInvocation.MyCommand.Path

Get-ChildItem -path $folderpath -recurse -include "*.doc" | % {
    $path =  ($_.fullname).substring(0,($_.FullName).lastindexOf("."))
    $doc = $word.documents.open($_.fullname)
    $doc.saveas($path, $wdFormatPDF) 
    $doc.close()
}

$word.Quit()
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16534292

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档