如何自动将 PowerPoint 转换为 PDF?

如何自动将 PowerPoint 转换为 PDF?

我需要使用第三方产品通过命令行将.ppt/.pptx 文件转换为.pdf 文件(或图像)。

我正在将它用于 Windows 2008 服务器,并且我不能使用任何 GUI 或网站,因为这需要自动化过程。

我尝试过 libreoffice,但是它在转换智能艺术时存在问题。

编辑:我的最终解决方案是使用powerpoint 与 C# 的互操作。 也可以看看:https://stackoverflow.com/questions/26372020/how-to-programmatically-create-a-powerpoint-from-a-list-of-images

答案1

无需第三方产品。如您所见,PowerPoint 可以将演示文稿导出为 PDF。只需应用一些脚本,您就可以实现您的结果。我在下面编写了 VB 脚本。只需创建一个名称以“.vbs”结尾的文件,然后粘贴下面的代码。

使用方法:

 CSCRIPT ppt.vbs "input file name" "output file name"

需要注意的是:

  • 如果名称包含空格,则需要用引号引起来。
  • 如果您没有指定输出文件的路径,PowerPoint 会将其放在您的 Documents 文件夹中。

我已添加内联链接以供参考各个部分。

Option Explicit

Sub WriteLine ( strLine )
    WScript.Stdout.WriteLine strLine
End Sub

' http://msdn.microsoft.com/en-us/library/office/aa432714(v=office.12).aspx
Const msoFalse = 0   ' False.
Const msoTrue = -1   ' True.

' http://msdn.microsoft.com/en-us/library/office/bb265636(v=office.12).aspx
Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.

' http://msdn.microsoft.com/en-us/library/office/ff746754.aspx
Const ppFixedFormatTypeXPS = 1  ' XPS format
Const ppFixedFormatTypePDF = 2  ' PDF format

' http://msdn.microsoft.com/en-us/library/office/ff744564.aspx
Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

' http://msdn.microsoft.com/en-us/library/office/ff744185.aspx
Const ppPrintOutputSlides = 1               ' Slides
Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
Const ppPrintOutputNotesPages = 5           ' Notes Pages
Const ppPrintOutputOutline = 6              ' Outline
Const ppPrintOutputBuildSlides = 7          ' Build Slides
Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts

' http://msdn.microsoft.com/en-us/library/office/ff745585.aspx
Const ppPrintAll = 1            ' Print all slides in the presentation.
Const ppPrintSelection = 2      ' Print a selection of slides.
Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
Const ppPrintSlideRange = 4     ' Print a range of slides.
Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.

' http://msdn.microsoft.com/en-us/library/office/ff744228.aspx
Const ppShowAll = 1             ' Show all.
Const ppShowNamedSlideShow = 3  ' Show named slideshow.
Const ppShowSlideRange = 2      ' Show slide range.

'
' This is the actual script
'

Dim inputFile
Dim outputFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso

If WScript.Arguments.Count <> 2 Then
    WriteLine "You need to specify input and output files."
    WScript.Quit
End If

inputFile = WScript.Arguments(0)
outputFile = WScript.Arguments(1)

Set objFso = CreateObject("Scripting.FileSystemObject")

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If

If objFso.FileExists( outputFile ) Then
    WriteLine "Your output file (' & outputFile & ') already exists!"
    WScript.Quit
End If

WriteLine "Input File:  " & inputFile
WriteLine "Output File: " & outputFile

Set objPPT = CreateObject( "PowerPoint.Application" )

objPPT.Visible = True
objPPT.Presentations.Open inputFile

Set objPresentation = objPPT.ActivePresentation
Set objPrintOptions = objPresentation.PrintOptions

objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType = ppShowAll

' Reference for this at http://msdn.microsoft.com/en-us/library/office/ff746080.aspx
objPresentation.ExportAsFixedFormat outputFile, ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoTrue, ppPrintHandoutHorizontalFirst, ppPrintOutputSlides, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False

objPresentation.Close
ObjPPT.Quit

答案2

您可以打印到 PDF 打印机驱动程序,例如 Adob​​e Distiller,或者许多更便宜甚至开源的驱动程序。

答案3

Office PowerPoint 查看器 2007有一个命令行开关/p,可以让您将 PowerPoint 文件打印到默认打印机。

例如:

Send the presentation to a printer, and print the file.

Example: "c:\program files\microsoft office\office12\PPTVIEW.exe" /P "Presentation.pptx"

This example prints the Presentation.pptx file.

PDF 打印机可能必须设置为您的默认打印机。

我建议你使用 Adob​​e Distiller,而不是购买 Adob​​e AcrobatPDF创建器。它是免费的,如果您调整选项,它可以让您自动保存输出文件。这样,您就可以使用完全命令行方法将 PowerPoint 文件转换为 PDF,而无需向 Microsoft 或 Adob​​e 支付额外费用。

答案4

用这个写了一个转换整个文件夹的脚本,如果可以改进请回复这是我第一次写 vbscript!

命令:

cscript scriptname.vbs "C:/path/to/folder"

文件将保存在脚本所在的目录中。

代码:

Option Explicit

Sub WriteLine ( strLine )
    WScript.Stdout.WriteLine strLine
End Sub

Const msoFalse = 0   ' False.
Const msoTrue = -1   ' True.

Const ppFixedFormatIntentScreen = 1 ' Intent is to view exported file on screen.
Const ppFixedFormatIntentPrint = 2  ' Intent is to print exported file.

Const ppFixedFormatTypeXPS = 1  ' XPS format
Const ppFixedFormatTypePDF = 2  ' PDF format

Const ppPrintHandoutVerticalFirst = 1   ' Slides are ordered vertically, with the first slide in the upper-left corner and the second slide below it.
Const ppPrintHandoutHorizontalFirst = 2 ' Slides are ordered horizontally, with the first slide in the upper-left corner and the second slide to the right of it.

Const ppPrintOutputSlides = 1               ' Slides
Const ppPrintOutputTwoSlideHandouts = 2     ' Two Slide Handouts
Const ppPrintOutputThreeSlideHandouts = 3   ' Three Slide Handouts
Const ppPrintOutputSixSlideHandouts = 4     ' Six Slide Handouts
Const ppPrintOutputNotesPages = 5           ' Notes Pages
Const ppPrintOutputOutline = 6              ' Outline
Const ppPrintOutputBuildSlides = 7          ' Build Slides
Const ppPrintOutputFourSlideHandouts = 8    ' Four Slide Handouts
Const ppPrintOutputNineSlideHandouts = 9    ' Nine Slide Handouts
Const ppPrintOutputOneSlideHandouts = 10    ' Single Slide Handouts

Const ppPrintAll = 1            ' Print all slides in the presentation.
Const ppPrintSelection = 2      ' Print a selection of slides.
Const ppPrintCurrent = 3        ' Print the current slide from the presentation.
Const ppPrintSlideRange = 4     ' Print a range of slides.
Const ppPrintNamedSlideShow = 5 ' Print a named slideshow.

Const ppShowAll = 1             ' Show all.
Const ppShowNamedSlideShow = 3  ' Show named slideshow.
Const ppShowSlideRange = 2      ' Show slide range.

'
' This is the actual script
'

Dim inputDirectory
Dim inputFolder
Dim inFiles
Dim outputFolder
Dim inputFile
Dim outputFile
Dim curFile
Dim objPPT
Dim objPresentation
Dim objPrintOptions
Dim objFso
Dim curDir



If WScript.Arguments.Count <> 1 Then
    WriteLine "You need to specify input files."
    WScript.Quit
End If

Set objFso = CreateObject("Scripting.FileSystemObject")

curDir = objFso.GetAbsolutePathName(".")

Set inputFolder = objFSO.GetFolder(WScript.Arguments.Item(0))
Set outputFolder = objFSO.GetFolder(WScript.Arguments.Item(0)) 

Set inFiles = inputFolder.Files

Set objPPT = CreateObject( "PowerPoint.Application" )

For Each curFile in inFiles

Set inputFile = curFile

If Not objFso.FileExists( inputFile ) Then
    WriteLine "Unable to find your input file " & inputFile
    WScript.Quit
End If

objPPT.Visible = TRUE
objPPT.Presentations.Open inputFile

Set objPresentation = objPPT.ActivePresentation
Set objPrintOptions = objPresentation.PrintOptions

objPrintOptions.Ranges.Add 1,objPresentation.Slides.Count
objPrintOptions.RangeType = ppShowAll

objPresentation.ExportAsFixedFormat curDir & curFile.Name & ".pdf", ppFixedFormatTypePDF, ppFixedFormatIntentScreen, msoTrue, ppPrintHandoutHorizontalFirst, ppPrintOutputSlides, msoFalse, objPrintOptions.Ranges(1), ppPrintAll, "Slideshow Name", False, False, False, False, False

objPresentation.Close

Next

ObjPPT.Quit

相关内容