Powershell转换选项卡到HTML邮件中的空格
我创建了一个将docx文件转换为html文件的脚本。 html文件通过电子邮件以html文本的形式发送。 要将文档转换为html,我这样做Powershell转换选项卡到HTML邮件中的空格,powershell,character-encoding,Powershell,Character Encoding,我创建了一个将docx文件转换为html文件的脚本。 html文件通过电子邮件以html文本的形式发送。 要将文档转换为html,我这样做 function ConvertDocToHtml { [cmdletbinding()] param($DocFile,[string]$HTMLTemplate) # Fix Type Error Add-Type -AssemblyName Microsoft.Office.Interop.Word $saveFormat
function ConvertDocToHtml {
[cmdletbinding()]
param($DocFile,[string]$HTMLTemplate)
# Fix Type Error
Add-Type -AssemblyName Microsoft.Office.Interop.Word
$saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Word.WdSaveFormat], "wdFormatFilteredHTML");
$word = new-object -comobject word.application
$word.Visible = $False
$opendoc = $word.documents.open($DocFile.FullName);
$opendoc.saveas($HTMLTemplate, $saveFormat);
$opendoc.close();
$word.quit();
}
为了在发送电子邮件之前修复HTML文本中的特殊字符,我使用了如下替换函数
$Body = Get-Content $HTMLFile -Raw
$BodyHTML = ReplaceChars $Body
function ReplaceChars {
[cmdletbinding()]
# EncodeHTMLSpecialChars
$Streng = $Streng.Replace("à", "à");
$Streng = $Streng.Replace("è", "è");
$Streng = $Streng.Replace("€", "€");
...
Send-MailMessage ... -Body $BodyHTML -BodyAsHtml
一切正常,但当我在docx文件中使用选项卡或多个空格时,电子邮件文本会显示这些字符
Total         € 100,00
我试图以这种方式皈依,但没有成功
$Streng = $Streng.Replace("Â", " ");
我怎样才能解决这个问题?
谢谢,Word的另存为HTML将用160/0xa0替换制表符 替换这些字符可以用.Replace完成,但是当每个选项卡扩展到几个空格时,一个正则表达式就可以了。这样,
# Replace no-break spaces with ordinary spaces
[regex]::replace($Streng , '\xa0+', ' ')
感谢您的解决方案,但通过这种方式,我删除了字符,但丢失了正确的制表格式。看起来无中断空间字节0xc2 0xa0的UTF-8编码在某些地方被误解了;您可以尝试添加-编码Utf8以获取内容和/或发送邮件消息。