我需要一个 PowerShell 脚本来使用 Microsoft Word 将 HTML 文档转换为 RTF。没有公开可用的片段对我有用。
这适用于 2023 年 Windows 10 22H2 上的 Microsoft Word 365 和 PowerShell 7.4.0。
将此脚本改编为其他格式:
剧本:
$ScriptLocation = Split-Path -Parent $Script:MyInvocation.MyCommand.Path
$InputFile="$ScriptLocation\source.htm"
$OutputFile="$ScriptLocation\target.rtf"
# First load MS Office base assemblies into the environment
Add-Type -Path $env:WINDIR\assembly\GAC_MSIL\office\*\office.dll #-PassThru # Use -PassThru to verify it worked (otherwise, fails silently)
# Then, add the target MS Word assembly
Get-ChildItem -Path $env:windir\assembly -Recurse -Filter Microsoft.Office.Interop.Word* -File | ForEach-Object {
Add-Type -LiteralPath ($_.FullName) #-PassThru # Use -PassThru to verify it worked (otherwise, fails silently)
}
# Doc open parameter array
$DOCOpen = @{}
$DOCOpen.FileName=$InputFile
$DOCOpen.ConfirmConversions=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.ReadOnly=[Microsoft.Office.Core.MsoTriState]::msoTrue
$DOCOpen.AddToRecentFiles=[Microsoft.Office.Core.MsoTriState]::msoFalse
$DOCOpen.PasswordDocument=""
$DOCOpen.PasswordTemplate=""
$DOCOpen.Revert=$true
$DOCOpen.WritePasswordDocument=""
$DOCOpen.WritePasswordTemplate=""
# Or use ::wdOpenFormatAuto
$DOCOpen.Format=[Microsoft.Office.Interop.Word.WdOpenFormat]::wdOpenFormatWebPages
$DOCOpen.Encoding=[Microsoft.Office.Core.MsoEncoding]::msoEncodingUTF8
$DOCOpen.Visible=$false
$DOCOpen.OpenAndRepair=$false
$DOCOpen.DocumentDirection=[Microsoft.Office.Interop.Word.WdDocumentDirection]::wdLeftToRight
$DOCOpen.NoEncodingDialog=$true
$DOCOpen.XMLTransform=""
# Create MS Office object
$appWord = New-Object -ComObject Word.Application
# Set application objecs not visible
$appWord.visible = $False
# Supress document macros
$appWord.AutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityForceDisable
# Supress alerts or dialogs
$appWord.DisplayAlerts = [Microsoft.Office.Interop.Word.WdAlertLevel]::wdAlertsNone
# Word specific settings
$appWord.ScreenUpdating = $False
$appWord.DisplayRecentFiles = $False
$appWord.DisplayScrollBars = $False
$SaveFormat = [Microsoft.Office.Interop.Word.WdSaveFormat]::wdFormatRTF
$DOCDocument = $appWord.Documents.OpenNoRepairDialog(
[ref]$DOCOpen.FileName,
[ref]$DOCOpen.ConfirmConversions,
[ref]$DOCOpen.ReadOnly,
[ref]$DOCOpen.AddToRecentFiles,
[ref]$DOCOpen.PasswordDocument,
[ref]$DOCOpen.PasswordTemplate,
[ref]$DOCOpen.Revert,
[ref]$DOCOpen.WritePasswordDocument,
[ref]$DOCOpen.WritePasswordTemplate,
[ref]$DOCOpen.Format,
[ref]$DOCOpen.Encoding,
[ref]$DOCOpen.Visible,
[ref]$DOCOpen.OpenAndRepair,
[ref]$DOCOpen.DocumentDirection,
[ref]$DOCOpen.NoEncodingDialog,
[ref]$DOCOpen.XMLTransform
)
$DOCDocument.SaveAs("$OutputFile", [ref]$SaveFormat)
$DOCDocument.Close()
$appWord.AutomationSecurity = [Microsoft.Office.Core.MsoAutomationSecurity]::msoAutomationSecurityByUI
$appWord.quit()
$rc = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($appWord)
这主要基于以下几点: