打开Word文档并使用PowerShell指定编码 [英] Open a word document and specify encoding with PowerShell
问题描述
我试图告诉PowerShell打开文本文件并选择某个编码选项.默认情况下,在Word中手动打开此文本文件时,它将尝试使用日语编码打开它,因此不会正确显示某些字符.
I'm trying to tell PowerShell to open a text file and choose a certain encoding option. By default, when opening this text file in Word manually, it tries to open it with Japanese encoding and so doesn't show certain characters correctly.
我尝试了很多不同的方法,但是没有任何效果,所以我完全被卡住了.
I've tried lots of different things but nothing works so I'm totally stuck.
除其他外,该文本文件每天需要转换为PDF.
This text file, amongst others, needs to be converted to PDF on a daily basis.
我当前的脚本如下:
$wdFormatPDF = 17
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$folderpath = "C:\Users\smirabile\Downloads\report-testing-destination\*"
$fileTypes = "accepted.spl"
Get-ChildItem -Path $folderpath -Include $fileTypes | ForEach-Object {
$path = ($_.FullName).Substring(0, ($_.FullName).LastIndexOf("."))
$doc = $word.Documents.Open($_.FullName)
$Word.Selection.PageSetup.Orientation = 1
$Word.Selection.PageSetup.LeftMargin = 20
$Word.Selection.PageSetup.RightMargin = 20
$doc.Select()
$Word.Selection.Font.Size = 9
$doc.SaveAs([ref]$path, [ref]$wdFormatPDF)
$doc.Close()
}
$word.Quit()
Stop-Process -Name WINWORD -Force
推荐答案
如有疑问,请阅读文档:
语法
expression.Open(FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert, WritePasswordDocument,
WritePasswordTemplate, Format, Encoding, Visible, OpenConflictDocument,
OpenAndRepair, DocumentDirection, NoEncodingDialog)
[…]
编码 |可选|变体 |当您查看保存的文档时,Microsoft Word将使用的文档编码(代码页或字符集).可以是任何有效的 MsoEncoding 常量.[…]默认值为系统代码页.
[…]
Encoding | Optional | Variant | The document encoding (code page or character set) to be used by Microsoft Word when you view the saved document. Can be any valid MsoEncoding constant. […] The default value is the system code page.
将 [Type] :: Missing
用于要使用其默认值的参数值.
Use [Type]::Missing
for parameters that you want used with their default value.
示例(使用iso-8859-15编码):
Example (using iso-8859-15 encoding):
$def = [Type]::Missing
$doc = $word.Documents.Open(
$_.FullName, $def, $def, $def, $def, $def, $def, $def, $def, $def, 28605
)
这篇关于打开Word文档并使用PowerShell指定编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!