Skip to content

Instantly share code, notes, and snippets.

@lyshie
Last active July 2, 2019 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyshie/4abb0e723ccf7b5fecb1076aa7083e4a to your computer and use it in GitHub Desktop.
Save lyshie/4abb0e723ccf7b5fecb1076aa7083e4a to your computer and use it in GitHub Desktop.
Batch convert docx/doc to pdf with powershell (課程計畫上傳PDF使用)

如何使用 docx2pdf 工具將 docx/doc 檔案批次轉換為 pdf

  • 下載 docx2pdf
  • 解壓縮後,將 docx2pdf.cmddocx2pdf.ps1 檔案放置於最上層目錄
  • 執行 docx2pdf.cmd
powershell -noexit -executionpolicy bypass -File "docx2pdf.ps1"
# Original from: https://gist.github.com/alexisnomine/9408777
# https://stackoverflow.com/questions/19683973
#
# Author: lyshie (lyshie@tn.edu.tw)
$FilePath = Get-Location
$Files = Get-ChildItem -Path "$FilePath" -Filter "*.doc*" -Recurse
$Word = New-Object -ComObject Word.Application
Foreach ($File in $Files) {
Write-Host "[DOCX2PDF]" $File.FullName
# open a Word document, filename from the directory
$Doc = $Word.Documents.Open($File.FullName, $False, $True)
# Swap out DOCX/DOC with PDF in the Filename
$Name=($Doc.FullName) -replace '.doc(x)*?$', ".pdf"
# Save this File as a PDF in Word 2010/2013
$Doc.SaveAs([ref] $Name, [ref] 17)
$Doc.Close([ref] [Microsoft.Office.Interop.Word.WdSaveOptions]::wdDoNotSaveChanges)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment