我還沒有找到解決辦法。
請僅使用 Powershell 的解決方案。如何在不使用 COM 物件的情況下將 Word *.docx 檔案轉換為 PDF *.pdf 檔案。這是通過 Windows 任務計劃在虛擬服務器上運行,并勾選“運行用戶是否登錄”選項,這就是當前 COM 物件解決方案不起作用的原因(如果我改為勾選“僅在用戶登錄時運行” ' 它有效,但它不是一個合適的選擇)。
我找到了一個 PSWriteWord powershell 模塊來搜索和替換部分代碼(word 模板到新 word 檔案),但它似乎不支持另存為 PDF。還有一個 PSWritePDF 模塊,但它似乎沒有這個功能。
$filePath = "C:\Temp6"
$origFile = "$filePath\Template.docx"
$newFile = "$filePath\ReplacedText.docx"
# Get the original document
$WordDocument = Get-WordDocument -FilePath $origFile
# Search and replace some text
ForEach ($Paragraph in $WordDocument.Paragraphs) {
$Paragraph.ReplaceText('[given_name]','Jane')
$Paragraph.ReplaceText('[surname]','Doe')
}
# Save as new document
Save-WordDocument -WordDocument $WordDocument -FilePath $newFile
# Quick check to confirm Word application is not left open
Get-Process -name WinWord -ErrorAction SilentlyContinue
# How to convert/save as PDF WITHOUT COM Object?
謝謝你。
uj5u.com熱心網友回復:
感謝@Tomalak,OpenOffice/LibreOffice 做到了這一點,這適用于 Windows 任務計劃,并勾選了“無論用戶是否登錄都運行”。
謝天謝地,我的 word 檔案沒有太多格式設定,因為我讀過多篇帖子說 OpenOffice/LibreOffice 轉換不合適,因為它去掉了一些格式。
# Set an alias for LibreOffice (OpenOffice)
Set-Alias -Name soffice "C:\Program Files\LibreOffice\program\soffice.exe"
# Export docx to pdf using LibreOffice (OpenOffice)
# Wait for the process to complete by using & and | Out-<StreamType>
& soffice --headless --convert-to pdf:writer_pdf_Export --outdir "$($filePath)" "$($newFile)" | Out-Null
# Remove the temporary word document
Remove-Item -Path $($newFile)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/345235.html
