gpt4 book ai didi

powershell - 如何在 PowerShell 中创建 MS Word 文档并将段落/行间距设置为无?

转载 作者:行者123 更新时间:2023-12-04 23:22:14 27 4
gpt4 key购买 nike

我正在寻找创建一个 PowerShell 脚本来生成一个 Word 文档。它按预期工作,但是当我运行脚本时,“我的文档:标题”和“日期:01-07-2014”之间有一个空格或换行符。结果如下所示:

My Document: Title

Date: 01-07-2014

我希望它看起来像这样:
My Document: Title
Date: 01-07-2014

如何在此脚本中写入一种删除段落中空格的方法?我的意思是,我想在 PowerShell 中将段落间距设置为单个(而不是在 Word 中作为默认值)顺便说一句,如果我不在“日期:$date”之前添加 $selection.TypeParagraph(),则结果如下所示:
My Document: TitleDate: 01-07-2014

就像,根本没有回车。目标是有一个回车,但在回车后没有空格。这是脚本。
$date = get-date -format MM-dd-yyyy
$filePath = "C:\users\myuser\file"

[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $true
$doc = $word.documents.add()

$selection = $word.selection
$selection.font.size = 14
$selection.font.bold = 1
$selection.typeText("My Document: Title")

$selection.TypeParagraph()
$selection.font.size = 11
$selection.typeText("Date: $date")

$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)

最佳答案

我认为您想要的是将段落样式从“正常”更改为“无间距”。

您的代码的开头是相同的:

$date = get-date -format MM-dd-yyyy
$filePath = "C:\users\myuser\file"

[ref]$SaveFormat = "microsoft.office.interop.word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $true
$doc = $word.documents.add()

然后您想将段落样式更改为“无间距”
$selection = $word.selection
$selection.WholeStory
$selection.Style = "No Spacing"

然后你可以继续你的其余代码:
$selection.font.size = 14
$selection.font.bold = 1
$selection.typeText("My Document: Title")

$selection.TypeParagraph()
$selection.font.size = 11
$selection.typeText("Date: $date")

$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)

关于powershell - 如何在 PowerShell 中创建 MS Word 文档并将段落/行间距设置为无?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977984/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com