à la lettre

ラカン派精神分析・精神病理学に関するいろいろ

Wordファイル(.docx)をPDFに変換するバッチファイル

目次


ドラッグ&ドロップでファイルを渡すと、同じフォルダにPDFができます(常に上書きされます)。
複数ファイルに対応しています。意外とないので便利。
powershell~$word.Quit()"までは一行です。

Wordファイル(.docx)をPDFに変換するバッチファイル

@echo off

:loop
if "%~1"=="" goto :end

echo %~1
echo PDFファイルに変換中です。このウィンドウは閉じないでください。

powershell -Command "$word=New-Object -ComObject Word.Application;$file='%~1';$doc=$word.Documents.OpenNoRepairDialog($file);$doc.SaveAs([ref] $file.Replace(\".docx\",\".pdf\"),[ref] 17);$doc.Close();$word.Quit()"

shift
goto loop

:end
exit /b

docx, doc, xlsx, xls, pptx, ppt に対応したバージョン

@echo off

set ARGS=%*
set ARGS=%ARGS:,=$comma$%
set ARGS=%ARGS:;=$semicolon$%
set ARGS=%ARGS: =$fullwidthsp$%

call :main %ARGS%

exit /b

:main

:loop
if "%~1"=="" goto :end

set ARG1=%~1
set ARG1=%ARG1:$comma$=,%
set ARG1=%ARG1:$semicolon$=;%
set ARG1=%ARG1:$fullwidthsp$= %

if exist "%ARG1%" (
echo %ARG1%
echo PDFファイルに変換中です。このウィンドウは閉じないでください。
) else (
echo %ARG1%
echo ファイルが存在しないか、ファイル名に処理できない記号等が含まれており、変換できません。何かキーを押してください。
pause
shift
goto loop
)

if %~x1==.docx (
powershell -Command "$app=New-Object -ComObject Word.Application;$file='%ARG1%';$doc=$app.Documents.OpenNoRepairDialog($file);$doc.SaveAs([ref] $file.Replace(\".docx\",\".pdf\"),[ref] 17);$doc.Close();$app.Quit()"
)

if %~x1==.doc (
powershell -Command "$app=New-Object -ComObject Word.Application;$file='%ARG1%';$doc=$app.Documents.OpenNoRepairDialog($file);$doc.SaveAs([ref] $file.Replace(\".doc\",\".pdf\"),[ref] 17);$doc.Close();$app.Quit()"
)

if %~x1==.xlsx (
powershell -Command "$app=New-Object -ComObject Excel.Application;$file='%ARG1%';$doc=$app.Workbooks.Open($file);$doc.ExportAsFixedFormat([ref] 0, [ref] $file.Replace(\".xlsx\",\".pdf\"));$doc.Close();$app.Quit()"
)

if %~x1==.xls (
powershell -Command "$app=New-Object -ComObject Excel.Application;$file='%ARG1%';$doc=$app.Workbooks.Open($file);$doc.ExportAsFixedFormat([ref] 0, [ref] $file.Replace(\".xls\",\".pdf\"));$doc.Close();$app.Quit()"
)

if %~x1==.pptx (
powershell -Command "$app=New-Object -ComObject Powerpoint.Application;$file='%ARG1%';$doc=$app.Presentations.Open($file);$doc.SaveAs([ref] $file.Replace(\".pptx\",\".pdf\"), [ref] 32);$doc.Close();$app.Quit()"
)

if %~x1==.ppt (
powershell -Command "$app=New-Object -ComObject Powerpoint.Application;$file='%ARG1%';$doc=$app.Presentations.Open($file);$doc.SaveAs([ref] $file.Replace(\".ppt\",\".pdf\"), [ref] 32);$doc.Close();$app.Quit()"
)

shift
echo;
goto loop

:end

exit /b