powershell word 转 pdf [英] powershell word to pdf

查看:35
本文介绍了powershell word 转 pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了其他人在这个论坛上的内容,基于成功使用 powershell Excel 到 pdf 没有成功.关于我在以下代码中失败的任何想法?我的目标是能够在不打开本机应用程序的情况下将整个文件夹的 doc 和 docx 文档转换为 pdf.提前致谢:

I modified what someone else had on this forum, based on a successful use of powershell Excel to pdf with no success. Any ideas on my failing in the following code? My goal is to be able to convert an entire folder of doc and docx documents to pdf without opening the native applications. Thanks in advance:

# 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()

推荐答案

$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()

这将遍历文件夹,然后查找 .doc 和 .docx 文件

This will Go trough the folder and then look for .doc and .docx files

然后打开每个文件创建一个新的保存名称并导出为 PDF

It then opens each file creates a new save name and exports to PDF

您可以从以下网址了解有关 ExportAsFixedFormat 的更多信息https://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx

You can learn more about the ExportAsFixedFormat from https://msdn.microsoft.com/en-us/library/bb256835(v=office.12).aspx

这篇关于powershell word 转 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆