【问题标题】:Problems changing encoding [duplicate]更改编码的问题[重复]
【发布时间】:2020-07-03 16:12:44
【问题描述】:

我正在编写一个 PowerShell 脚本来将 docx 转换为 HTML,并更改 HTML 的编码,因为默认情况下它将其保存为 windows-1252。

我需要这个,因为稍后我将这个 HTML 保存为电子邮件的正文,也由 PowerShell 发送。因为我是西班牙人,所以我需要显示口音和波浪号(现在显示为 ?)。

我尝试了带有所有参数的SaveAs 方法,但无法正常工作。

这是我的脚本:

$MSWord = New-Object -ComObject Word.Application
$MSWord.Documents.Open(“C:\Users\USER\Videos\CAMBIO_TURNO.docx”)
$MSWord.Visible = $false

# Save HTML
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], “wdFormatHTML”);
$path = “C:\Users\USER\Videos\CAMBIO_TURNO.html”
$MSWord.ActiveDocument.SaveAs([ref]$path, [ref]$saveFormat)

# Close File

$MSWord.ActiveDocument.Close()
$MSWord.Quit()

然后,为了将它发送给我,我在 PowerShell 上使用了其他代码:

$OutputEncoding = [System.Text.Encoding]::UTF8

$body = [IO.File]::ReadAllText(“C:\Users\USER\Videos\CAMBIO_TURNO.html”)

Send-MailMessage -To “EMAIL@EMAIL” -From “EMAIL@EMAIL” -Subject “CAMBIO” -Body $body -Encoding $OutputEncoding -BodyAsHtml -Attachments “C:\Users\USER\Videos\CAMBIO_TURNO.xlsx” -Dno onSuccess, onFailure -SmtpServer smtp.gmail.com -Credential EMAIL@EMAIL

第二次更新

(虽然我去了标记为重复的页面:Word Document.SaveAs ignores encoding, when calling through OLE, from Ruby or VBS它并没有解决我的问题。那个字配置不起作用)

以下是我使用网络选项将文档保存为 utf-8 后的尝试:

#DEFINE outputencoding FOR THE CONSOLE - IT SEEMS THAT IT DOESN'T WORK. I typed ñ and ó and they appear as ?? becasue it doesn't convert the hexadecimal values to the right charset
$OutputEncoding= New-Object -typename System.Text.ASCIIEncoding

# Open word to add input into the signature file
$MSWord = New-Object -ComObject word.application
$MSWord.Documents.Open('C:\Users\USER\Videos\CAMBIO_TURNO.docx')

 # Save HTML
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], 'wdFormatFilteredHTML');

$path = 'C:\Users\USER\Videos\CAMBIO_TURNO.html'

$default = [Type]::Missing
$MSWord.ActiveDocument.SaveAs2([ref]$path, [ref]$saveFormat, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]$default, [ref]28591)

# Close File
$MSWord.ActiveDocument.Close()
$MSWord.Quit()

$HTMLw = Get-Content -Path 'C:\Users\USER\Videos\CAMBIO_TURNO.html' -Encoding ASCII -Force
$HTMLw -replace 'charset=windows-1252','charset=ISO-8859-1' | Set-Content -Path 'C:\Users\USER\Videos\CAMBIO_TURNO.html' -Encoding ASCII -Force

【问题讨论】:

    标签: powershell ms-word


    【解决方案1】:

    一方面,您应该避免使用印刷引号 ()。始终在代码中使用直引号 (")。

    话虽如此,您面临的问题很可能是传递带有符号常量名称的字符串不起作用。要么使用常量的numeric value,要么自己定义一个常量:

    New-Variable -Name wdFormatHTML -Value 8 -Option Constant
    $MSWord.ActiveDocument.SaveAs($path, $wdFormatHTML)
    

    或者,您应该能够解析常量 via the Interop API,但我现在手头没有 Office 安装,所以我无法测试。

    您在保存时也没有指定所需的输出文件编码。

    New-Variable -Name wdFormatHTML -Value 8 -Option Constant
    $default = [Type]::Missing
    $MSWord.ActiveDocument.SaveAs($path, $wdFormatHTML, $default, $default, $default, $default, $default, $default, $default, $default, $default, 65001)
    

    【讨论】:

    • 哇!我要试试!太感谢了。它尝试做可变的事情,但我猜我做错了。我写了$def = [ref]::missing,显然没有用。
    • 嗨!我试过你发给我的。但它不会将 html 文档转换为 UFT8 或 LATIN1(输入代码时)。这是 HTML 的头部:<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"> <head> <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> &notepad++ 显示的关于 HTML 的编码也是“Windows-1252”。不过,感谢您的宝贵时间
    • 尝试将文件另存为wdFormatFilteredHTML(数值10)。如果这也没有相应地调整元标记,您可能需要更改导出 HTML 中的值。
    • 您好,首先:感谢您的帮助。我试过这个New-Variable -Name wdFormatHTML -Value 10 -Option Constant,但它总是抛出同样的错误(即使我删除了“-Option Constant”部分)。错误:can't overwrite wdFormatHTML because is read-only or constant。所以,然后,我尝试了这个:$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML"); 但最后,编码和字符集始终是 w1252。
    • 你不能重新定义一个常量。如果可以的话,它就不会那么稳定了,不是吗?无论如何,它不必是一个常数。只需定义一个变量 $wdFormatFilteredHTML = 10 并使用它,如果您希望能够使用该值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2017-03-02
    • 2023-04-10
    • 2018-08-10
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多