8

我需要自动创建演示文稿(OpenOffice 或 Powerpoint)。演示文稿应采用给定目录中每个演示文稿的前两张幻灯片,然后将它们组合成一个演示文稿。我很困惑我应该采取什么方法来解决这个问题。任何指针将不胜感激。

4

5 回答 5

13

谈到 PowerPoint,您将使用 VBA 宏来完成这项工作,例如

Sub Pull()
Dim SrcDir As String, SrcFile As String

    SrcDir = PickDir()
    If SrcDir = "" Then Exit Sub

    SrcFile = Dir(SrcDir & "\*.ppt")

    Do While SrcFile <> ""
        ImportFromPPT SrcDir + "\" + SrcFile, 1, 2
        SrcFile = Dir()
    Loop

End Sub

选择您的源目录,您可以使用此功能

Private Function PickDir() As String
Dim FD As FileDialog

    PickDir = ""

    Set FD = Application.FileDialog(msoFileDialogFolderPicker)
    With FD
        .Title = "Pick a directory to work on"
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count <> 0 Then
            PickDir = .SelectedItems(1)
        End If
    End With

End Function

现在 - 重点是在保留源格式的同时插入另一个 PPT 中的幻灯片。这是一件棘手的事情,因为PPT VBAInsertFromFile方法没有用处。微软给了我们很好的时间在无数的 20 小时调试会话中找出困难的方法:-) 你需要输入大量代码才能正确完成它 - 比手动使用对话复杂得多,特别是如果你的源幻灯片偏离您的源母版幻灯片。

如果您的 PPT 坚持自己的主人,您可以放心地省略“>>>>”之间的所有代码

Private Sub ImportFromPPT(FileName As String, SlideFrom As Long, SlideTo As Long)
Dim SrcPPT As Presentation, SrcSld As Slide, Idx As Long, SldCnt As Long

    Set SrcPPT = Presentations.Open(FileName, , , msoFalse)
    SldCnt = SrcPPT.Slides.Count

    If SlideFrom > SldCnt Then Exit Sub
    If SlideTo > SldCnt Then SlideTo = SldCnt

    For Idx = SlideFrom To SlideTo Step 1
        Set SrcSld = SrcPPT.Slides(Idx)
        SrcSld.Copy
        With ActivePresentation.Slides.Paste
            .Design = SrcSld.Design
            .ColorScheme = SrcSld.ColorScheme
            ' if slide is not following its master (design, color scheme)
            ' we must collect all bits & pieces from the slide itself

            ' >>>>>>>>>>>>>>>>>>>>

            If SrcSld.FollowMasterBackground = False Then
                .FollowMasterBackground = False
                .Background.Fill.Visible = SrcSld.Background.Fill.Visible
                .Background.Fill.ForeColor = SrcSld.Background.Fill.ForeColor
                .Background.Fill.BackColor = SrcSld.Background.Fill.BackColor

                ' inspect the FillType object
                Select Case SrcSld.Background.Fill.Type
                    Case Is = msoFillTextured
                        Select Case SrcSld.Background.Fill.TextureType
                        Case Is = msoTexturePreset
                            .Background.Fill.PresetTextured (SrcSld.Background.Fill.PresetTexture)
                        Case Is = msoTextureUserDefined
                        ' TextureName gives a filename w/o path
                        ' not implemented, see picture handling
                        End Select

                    Case Is = msoFillSolid
                        .Background.Fill.Transparency = 0#
                        .Background.Fill.Solid

                    Case Is = msoFillPicture
                        ' picture cannot be copied directly, need to export and re-import slide image
                        If SrcSld.Shapes.Count > 0 Then SrcSld.Shapes.Range.Visible = False
                        bMasterShapes = SrcSld.DisplayMasterShapes
                        SrcSld.DisplayMasterShapes = False
                        SrcSld.Export SrcPPT.Path & SrcSld.SlideID & ".png", "PNG"

                        .Background.Fill.UserPicture SrcPPT.Path & SrcSld.SlideID & ".png"
                        Kill (SrcPPT.Path & SrcSld.SlideID & ".png")

                        SrcSld.DisplayMasterShapes = bMasterShapes
                        If SrcSld.Shapes.Count > 0 Then SrcSld.Shapes.Range.Visible = True

                    Case Is = msoFillPatterned
                        .Background.Fill.Patterned (SrcSld.Background.Fill.Pattern)

                    Case Is = msoFillGradient

                        ' inspect gradient type
                        Select Case SrcSld.Background.Fill.GradientColorType
                        Case Is = msoGradientTwoColors
                            .Background.Fill.TwoColorGradient
                                SrcSld.Background.Fill.GradientStyle , _
                                SrcSld.Background.Fill.GradientVariant
                        Case Is = msoGradientPresetColors
                            .Background.Fill.PresetGradient _
                                SrcSld.Background.Fill.GradientStyle, _
                                SrcSld.Background.Fill.GradientVariant, _
                                SrcSld.Background.Fill.PresetGradientType
                        Case Is = msoGradientOneColor
                            .Background.Fill.OneColorGradient _
                                SrcSld.Background.Fill.GradientStyle, _
                                SrcSld.Background.Fill.GradientVariant, _
                                SrcSld.Background.Fill.GradientDegree
                        End Select

                    Case Is = msoFillBackground
                        ' Only shapes - we shouldn't come here
                End Select
            End If

            ' >>>>>>>>>>>>>>>>>>>>

        End With
    Next Idx

End Sub

该代码不会检查只读或受密码保护的文件,并且会在它们上崩溃。还要注意不要超过收集器文件本身。否则它应该工作。我必须承认我已经很长时间没有查看代码了 ;-)

于 2011-03-18T11:48:39.057 回答
1

你可以谷歌“powerpoint join”来找到一个有用的工具来加入许多ppts。

于 2017-11-08T13:06:58.190 回答
0

我很高兴@miked能够为您提供所需的东西。

如果使用 .NET,另一种方法将在本文中讨论

于 2018-03-08T05:37:34.750 回答
0

一个简单快速的解决方案:

I := Presentation.Slides.InsertFromFile(FileName,X,StartSlideNo,EndSlideNo);
Presentation.Slides.Item(I).ApplyTheme(FileName);
Presentation.Slides.Item(I).ApplyTemplate(FileName);

注意:X 是在演示文稿中插入幻灯片的地方

我是插入幻灯片的实际位置

代码是用 Delphi/Pascal 编写的,但您可以轻松地转换它...

于 2019-04-20T09:41:33.107 回答
0

您可以使用 Aspose.Slides for .NET 来完成此操作。它甚至允许将 OpenOffice 和 PowerPoint 演示文稿连接在一起。查看这篇文章

var presentation1 = new Presentation("presentation1.pptx");
var presentation2 = new Presentation("presentation2.odp");

var mergedPresentation = new Presentation();
while (mergedPresentation.Slides.Count > 0) mergedPresentation.Slides.RemoveAt(0);

// Adding two slides from the first PPTX presentation
mergedPresentation.Slides.AddClone(presentation1.Slides[0]);
mergedPresentation.Slides.AddClone(presentation1.Slides[1]);

// Adding two slides from the second OPD presentation
mergedPresentation.Slides.AddClone(presentation2.Slides[0]);
mergedPresentation.Slides.AddClone(presentation2.Slides[1]);

mergedPresentation.Save("mergedPresentation.pptx", SaveFormat.Pptx);
于 2020-02-06T14:33:14.740 回答