使用 Microsoft Word 将 HTML 转换为 RTF 的 PowerShell 脚本

问题描述 投票:0回答:1

我需要一个 PowerShell 脚本来使用 Microsoft Word 将 HTML 文档转换为 RTF。没有公开可用的片段对我有用。

html powershell ms-word rtf
1个回答
0
投票

这适用于 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)

这主要基于以下几点:

© www.soinside.com 2019 - 2024. All rights reserved.