【问题标题】:powershell edit powerpoint slide notespowershell 编辑 powerpoint 幻灯片笔记
【发布时间】:2016-09-17 01:42:57
【问题描述】:

我正在尝试使用 PowerShell 以编程方式更新 PowerPoint 幻灯片笔记中的笔记。能够做到这一点将节省大量时间。下面的代码允许我使用 PowerShell 编辑注释字段,但每次都会弄乱格式。

$PowerpointFile = "C:\Users\username\Documents\test.pptx"
$Powerpoint = New-Object -ComObject powerpoint.application
$ppt = $Powerpoint.presentations.open($PowerpointFile, 2, $True, $False)
foreach($slide in $ppt.slides){
    if($slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -match "string"){
        $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -replace "string","stringreplaced"
    }
}
Sleep -Seconds 3
$ppt.Save()
$Powerpoint.Quit()

例如,现在它将遍历每张幻灯片的注释并将单词字符串更新为 stringreplaced,但随后整个注释文本变为粗体。在我的笔记中,我在笔记的顶部有一个单词是粗体的,然后是下面的文字。例如,幻灯片上的注释我看起来像这样:

笔记标题

帮我处理这个字符串。

PowerShell 更新 notes 字段后,会将其保存到新的 .pptx 文件中,但 note 现在看起来像这样:

笔记标题

帮我更换这个字符串。

关于如何在不弄乱笔记中的任何格式的情况下更新幻灯片笔记的任何想法?它只会弄乱脚本更新幻灯片的格式。

【问题讨论】:

    标签: powershell powerpoint


    【解决方案1】:

    当您在 PPT 中更改文本范围的整个文本内容时,正如您的代码所做的那样,更改后的文本范围将获取范围中第一个字符的格式。我不确定你会如何在 PowerShell 中执行此操作,但这里有一个 PPT VBA 中的示例,它演示了相同的问题,并展示了如何使用 PPT 自己的 Replace 方法来解决问题:

    Sub ExampleTextReplace()
    ' Assumes two shapes with text on Slide 1 of the current presentation
    ' Each has the text "This is some sample text"
    ' The first character of each is bolded
    
    ' Demonstrates the difference between different methods of replacing text
    '   within a string
    
        Dim oSh As Shape
    
        ' First shape: change the text
        Set oSh = ActivePresentation.Slides(1).Shapes(1)
        With oSh.TextFrame.TextRange
            .Text = Replace(.Text, "sample text", "example text")
        End With
        ' Result:  the entire text string is bolded
    
    
        ' Second shape: Use PowerPoint's Replace method instead
        Set oSh = ActivePresentation.Slides(1).Shapes(2)
        With oSh.TextFrame.TextRange
            .Replace "sample text", "example text"
        End With
        ' Result:  only the first character of the text is bolded
        '          as it was originally
    
    End Sub
    

    【讨论】:

    • 使用此方法似乎会获取全部内容并替换它们。不是用“示例文本”替换单词“示例文本”,而是用“示例文本”替换整个 .Text。
    • 不在这里。也许 PowerShell 版本的翻译不太准确。发布您现在拥有的代码;或许更熟悉 PowerShell 的人可以看看。
    • 我知道有点老了,但这里有一个工作的 PowerShell sn-p:$presentation.Slides[1].Shapes[4].TextFrame.TextRange.Replace($f0a, $f1a)。做得很好,并且不会干扰形状中的其他格式。 (PowerShell 2016)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    相关资源
    最近更新 更多