gpt4 book ai didi

powershell - 将 ppt 转换为 PDF 时出现错误

转载 作者:行者123 更新时间:2023-12-02 14:50:02 30 4
gpt4 key购买 nike

有人可以指导我代码出了什么问题吗?我需要将 PowerPoint 文件转换为 PDF 文件。

代码:

#Convert Powerpoint formats to pdf
Param(
[string]$inputPath,
[string]$outputPath
)
Add-Type -AssemblyName Office
Add-Type -AssemblyName Microsoft.Office.Interop.PowerPoint
$ppFormatPDF = 32

$ppQualityStandard = 0
$pp = New-Object -ComObject PowerPoint.Application
# TODO: Why this property does not work
#$pp.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse

$ppt = $pp.Presentations.Open($inputPath)
$ppt.SaveAs($outputPath, $ppFormatPDF) # 32 is for PDF
$ppt.Close()
$pp.Quit()
$pp = $null
[gc]::Collect()
[gc]::WaitForPendingFinalizers()

错误:

Exception calling "SaveAs" with "2" argument(s): "Presentation.SaveAs :
PowerPoint can't save ^0 to ^1."
At D:\AllAquent\Rambo\Digo\war\WEB-INF\classes\resources\pptToPdf.ps1:17 char:12
+ $ppt.SaveAs <<<< ($outputPath, $opt) # 32 is for PDF
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ComMethodTargetInvocation

最佳答案

二手this script几个月前,在发布到这里之前删除了我自己的修改。希望这可以帮助你!

请注意,此脚本旨在将指定目录中的所有 Powerpoint 演示文稿转换为 PDF。

Function Convert-PptxToPDF { 

[CmdletBinding()]
Param(
$File,
$OutputFile
)

# add key assemblies
Add-type -AssemblyName office -ErrorAction SilentlyContinue
Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue

# Open PowerPoint
$ppt = new-object -com powerpoint.application
$ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse


# Open the $File presentation
$pres = $ppt.Presentations.Open($file)

# Now save it away as PDF
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
$pres.SaveAs($OutputFile,$opt)

# and Tidy-up
$pres.Close()
$ppt.Quit()
$ppt=$null

}

#Where your PDF will be saved
$OutputFile = "C:\Temp\"

# File-extension could be changed to .pptx if needed
Foreach ($File in $(ls $OutputFile -Filter "*.ppt")) {
# Build name of output file
$pathname = split-path $File
$filename = split-path $File -leaf
$rmfileext = $filename.split(".")[0]
$OutputFile = $pathname + $rmfileext + ".pdf"

# Convert _this_ file to PDF
Convert-PptxToPDF -file $File -OutputFile $OutputFile
}

我对这个脚本没有任何功劳。

关于powershell - 将 ppt 转换为 PDF 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34509052/

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