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读写)
  • 热门搜索
    关于食品安全的手抄报 建军节手抄报 小学生科技手抄报 关于母爱的手抄报 感恩母亲的手抄报 读书伴我成长手抄报 讲文明手抄报 读书节手抄报内容 语文手抄报图片 冬至手抄报