我想要以最快的方式匯出我的報告.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\""