powershell word至pdf
我修改了其他人在这个论坛上的内容,基于成功使用powershell Excel到pdf,但没有成功。有没有关于我在以下代码中失败的想法?我的目标是能够在不打开本机应用程序的情况下将整个doc和docx文档文件夹转换为pdf。提前感谢:powershell word至pdf,powershell,pdf,ms-word,Powershell,Pdf,Ms Word,我修改了其他人在这个论坛上的内容,基于成功使用powershell Excel到pdf,但没有成功。有没有关于我在以下代码中失败的想法?我的目标是能够在不打开本机应用程序的情况下将整个doc和docx文档文件夹转换为pdf。提前感谢: # Acquire a list of DOC files in a folder $Word = New-Object -ComObject word.application $path = "c:\Users\Desktop\Test" $formats
# Acquire a list of DOC files in a folder
$Word = New-Object -ComObject word.application
$path = "c:\Users\Desktop\Test"
$formats = "Microsoft.Office.Interop.Word.WdFixedFormatType" -as [type]
$Word.visible = $false
$fileTypes = "*.docx","*doc"
$Files = GET-CHILDITEM $path -include $fileTypes
Foreach ($File in $Files) {
$filepath = Join-Path -path $path -childpath ($File.basename +".pdf")
$Doc = $Word.open($File.fullname, 3)
$Doc.Saved= $true
"Converting $File to pdf ... $destPath"
# Save this File as a PDF in Word 2010/2013
$Doc.ExportAsFixedFormat($formats::wdTypePDF, $path)
$Doc.FullName -replace '\.doc?$', '.pdf'
$Doc.Close()
}
$Word.Quit()
这将遍历文件夹,然后查找.doc和.docx文件
然后打开每个文件,创建新的保存名称并导出为PDF
您可以从中了解有关ExportAsFixedFormat的更多信息
仅供参考,当您使用
Word.Application
comobject时,您正在打开本机应用程序。True,但$Word.visible=$false意味着应用程序仅在后台运行,我相信…您是否收到错误?$Word.open
$Word.Documents.open
,首先。而且您的Get ChildItem
不会返回任何内容,因此您永远不会输入foreach
循环。此外,$Formats等于nothingExportAsFixedFormat
:13个参数。不少。现在我明白了为什么我的ISE在这条线上疯狂了。。。始终阅读文档!%)参数编号5是WdExportRange
,用于确定导出的页面数量;我发现将其设置为3
,因为在回答中,我只得到了第一页,设置为0
将导出整个文档。
$path = 'C:\tests'
$wd = New-Object -ComObject Word.Application
Get-ChildItem -Path $path -Include *.doc, *.docx -Recurse |
ForEach-Object {
$doc = $wd.Documents.Open($_.Fullname)
$pdf = $_.FullName -replace $_.Extension, '.pdf'
$doc.ExportAsFixedFormat($pdf,17,$false,0,3,1,1,0,$false, $false,0,$false, $true)
$doc.Close()
}
$wd.Quit()