PowerShell 将Word文档批量转换成PDF文档

 时间:2024-10-13 09:16:38

1、打开Windows PowerShell ISE

PowerShell 将Word文档批量转换成PDF文档

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转为.pdffunction GetPdffile($wordfile){ $pdffile = '' if($wordfile.ToString().Contains('.docx')) { $pdffile = $wordfile.ToString().Replace('.docx','.pdf') } else { $pdffile = $wordfile.ToString().Replace('.doc','.pdf') } return $pdffile }

PowerShell 将Word文档批量转换成PDF文档

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

PowerShell 将Word文档批量转换成PDF文档PowerShell 将Word文档批量转换成PDF文档
  • 封装ExcelVBA代码:[1]动态链接库的生成
  • 如何使用python语言中的array进行访问数组
  • 在jQuery中如何添加HTML元素
  • 易语言怎么设计可以计算小数的加法程序?
  • C#应用 - 文件系统管理(文件流进行txt读写)
  • 热门搜索
    什么牌子的平板电脑好 纯洁的意思 曾经沧海难为水除却巫山不是云是什么意思 黄经是什么意思 轻歌曼舞是什么意思 trunk是什么意思 春运什么时候开始 贻笑大方是什么意思 延伸的意思 享誉世界的意思