レポートをエクスポートする最も速い方法が欲しい.docxファイルする.pdf新しい更新バージョンができたら、それを他の人に配布します。
これまでマウスを使用して手動で実行する必要があった次の手順を自動化するコマンドライン アプローチを探しています。
File -> Save as -> Browse for location
バッチ ファイルのコマンド オプションは何ですか?
答え1
Word 2013 でグローバル マクロを作成します。
' The Word macro for exporting to PDF (the Word window closes after finishing)
Sub ExportToPDFext()
ChangeFileOpenDirectory ThisDocument.Path
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".")) + "pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
From:=1, _
To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
Application.Quit SaveChanges:=wdDoNotSaveChanges
End Sub
その後、コマンド ラインで Word 文書を PDF に変換できます。
"C:\Program Files\Microsoft Office\Office15\WINWORD.EXE" /mExportToPDFext /q "your_document_path.docx"
Word ウィンドウは、マクロの実行が終了すると閉じるように設定されており、パラメータ /q によって Word の読み込み時にスプラッシュ ウィンドウが無効になるため、表示されません。
代替の詳細な手順は次のとおりですGitHubでまた、コンテキスト メニュー オプションを使用すると、コマンド ラインがなくてもバッチ変換を行うことができます。レジストリに追加できます。DOC および DOCX の場合:
[HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere]
@="Save PDF here"
[HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere\command]
@="\"C:\\Program Files\\Microsoft Office\\Office15\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""
[HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere]
@="Save PDF here"
[HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere\command]
@="\"C:\\Program Files\\Microsoft Office\\Office15\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""
答え2
バッチ変換を行うための簡単なコマンドラインツールとして、以下を使用できますdocx2pdf
。https://github.com/AlJohri/docx2pdf/
インストール:
pip install docx2pdf
走る:
docx2pdf myFolderOfWordDocs
免責事項: 私はこのツールの作成者です。
答え3
解決策はオレクシー・コフトゥン現在のOffice 16(Office 360)に採用されています。
Word マクロでは、ThisDocument.Path
に変更する必要がありましたActiveDocument.Path
。
' The Word macro for exporting to PDF (the Word window closes after finishing)
Sub ExportToPDFext()
ChangeFileOpenDirectory ActiveDocument.Path
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=Left(ActiveDocument.FullName, InStrRev(ActiveDocument.FullName, ".")) + "pdf", _
ExportFormat:=wdExportFormatPDF, _
OpenAfterExport:=False, _
OptimizeFor:=wdExportOptimizeForPrint, _
Range:=wdExportAllDocument, _
From:=1, _
To:=1, _
Item:=wdExportDocumentContent, _
IncludeDocProps:=True, _
KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, _
DocStructureTags:=True, _
BitmapMissingFonts:=True, _
UseISO19005_1:=False
Application.Quit SaveChanges:=wdDoNotSaveChanges
End Sub
レジストリの場合、Office 16 では若干異なるパスが使用されます。
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere]
@="Save PDF here"
[HKEY_CLASSES_ROOT\Word.Document.8\shell\SavePDFhere\command]
@="\"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""
[HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere]
@="Save PDF here"
[HKEY_CLASSES_ROOT\Word.Document.12\shell\SavePDFhere\command]
@="\"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE\" /mExportToPDFext /q \"%1\""