PowerShell 将Word文档批量转换成PDF文档
1、打开Windows PowerShell ISE

2、输入下面命令。
$dirPath = 'C:\test' # 要导成pdf的word文件所在目录地址
$wordToPdfLog = Join-Path -Path $dirPath -ChildPath "wordToPdfLog.txt" # 已导文件列表wordToPdfLog
$notWordFilelog = Join-Path -Path $dirPath -ChildPath "notWordFilelog.txt" # 目录下面不是word文件列表
$errorlog = Join-Path -Path $dirPath -ChildPath "errorlog.txt" # 错误日志
dir $dirPath | ForEach-Object {
WordConvertToPDF($_.FullName)
}
# 将word文件转换成pdf,并生成日志
function WordConvertToPDF($wordfile){
$pdffile = GetPdffile($wordfile)
try
{
if(![String]::IsNullOrEmpty($pdffile) -and $wordfile.ToString().Contains('.doc')){
$wordApp = New-Object -ComObject Word.Application
$document = $wordApp.Documents.Open($wordfile)
$document.SaveAs([ref] $pdffile, [ref] 17)
$document.Close()
$wordApp.Quit()
$wordfile + "`t>>>>>`t" + $pdffile >> $wordToPdfLog # 将转换的成功的word文件写入日志
}
else
{
$wordfile >> $notWordFilelog # 将非word文件写入日志
}
}
catch
{
$wordfile >> $errorlog # 错误日志
}
}
# 将.docx .doc转为.pdf
function GetPdffile($wordfile){
$pdffile = ''
if($wordfile.ToString().Contains('.docx'))
{
$pdffile = $wordfile.ToString().Replace('.docx','.pdf')
}
else
{
$pdffile = $wordfile.ToString().Replace('.doc','.pdf')
}
return $pdffile
}

3、点击运行脚本或直接按F5,把test目录下面的word转换成pdf。
如果第一次运行报错,就再运行一次。


声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
                                阅读量:53
阅读量:172
阅读量:146
阅读量:127
阅读量:24