Forked from equinox79/フォルダ内のdocxからpdf生成(pdfないものだけ)※右クリックして「Powershellで実行」.ps1
Last active
August 4, 2024 17:33
-
-
Save K4zuki/b6be0948b3fd8b247b6161e4155fbd98 to your computer and use it in GitHub Desktop.
docx2pdf.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$word = NEW-OBJECT -COMOBJECT WORD.APPLICATION | |
Write-Host "[Generate PDFs from DOCX's in current directory]" | |
$files = Get-ChildItem | Where-Object{ $_.Name -match "docx$" } | |
Write-Host "[Start process]" | |
foreach ($file in $files) | |
{ | |
try | |
{ | |
$result = (Test-Path $file.FullName.Replace(".docx", ".pdf")) | |
if ($result) | |
{ | |
Write-Host "$( $file.Name ) ... file exists. Skipping" | |
} | |
else | |
{ | |
Write-Host "$( $file.Name ) ... processing" | |
$doc = $word.Documents.OpenNoRepairDialog($file.FullName) | |
$doc.SaveAs([ref] $file.FullName.Replace(".docx", ".pdf"), [ref]17) | |
$doc.Close() | |
Write-Host "$($file.FullName.Replace(".docx", ".pdf") ) ... converted" | |
} | |
} | |
catch | |
{ | |
Write-Host "[ERROR]$( $file.Name ) ... conversion failed" | |
} | |
} | |
Write-Host "[Process ended]" | |
$word.Quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment