如何在 C# 或 Perl 中以编程方式打开 PowerPoint 演示文稿并将其保存为 HTML/JPEG?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I programmatically open and save a PowerPoint presentation as HTML/JPEG in C# or Perl?
提问by
I am looking for a code snippet that does just this, preferably in C# or even Perl.
我正在寻找一个可以做到这一点的代码片段,最好是在 C# 甚至 Perl 中。
I hope this not a big task ;)
我希望这不是一项大任务;)
回答by Sinan ünür
As Kevpoints out, don't use this on a web server. However, the following Perl script is perfectly fine for offline file conversion etc:
正如Kev指出的那样,不要在 Web 服务器上使用它。但是,以下 Perl 脚本非常适合离线文件转换等:
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft PowerPoint';
$Win32::OLE::Warn = 3;
use File::Basename;
use File::Spec::Functions qw( catfile );
my $EXPORT_DIR = catfile $ENV{TEMP}, 'ppt';
my ($ppt) = @ARGV;
defined $ppt or do {
my $progname = fileparse using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace PPInterop
{
class Program
{
static void Main(string[] args)
{
var app = new PowerPoint.Application();
var pres = app.Presentations;
var file = pres.Open(@"C:\Presentation1.ppt", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
file.SaveCopyAs(@"C:\presentation1.jpg", Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);
}
}
}
;
warn "Usage: $progname output_filename\n";
exit 1;
};
my $app = get_powerpoint();
$app->{Visible} = 1;
my $presentation = $app->Presentations->Open($ppt);
die "Could not open '$ppt'\n" unless $presentation;
$presentation->Export(
catfile( $EXPORT_DIR, basename $ppt ),
'JPG',
1024,
768,
);
sub get_powerpoint {
my $app;
eval { $app = Win32::OLE->GetActiveObject('PowerPoint.Application') };
die "$@\n" if $@;
unless(defined $app) {
$app = Win32::OLE->new('PowerPoint.Application',
sub { $_[0]->Quit }
) or die sprintf(
"Cannot start PowerPoint: '%s'\n", Win32::OLE->LastError
);
}
return $app;
}
回答by Dolphin
The following will open C:\presentation1.ppt
and save the slides as C:\Presentation1\slide1.jpg
etc.
以下将打开C:\presentation1.ppt
并将幻灯片另存为C:\Presentation1\slide1.jpg
等。
If you need to get the interop assembly, it is available under 'Tools' in the Office install program, or you can download it from here (office 2003). You should be able to find the links for other versions from there if you have a newer version of office.
如果您需要获取互操作程序集,它可以在 Office 安装程序的“工具”下找到,或者您可以从这里 (office 2003)下载。如果您有较新版本的 office,您应该能够从那里找到其他版本的链接。
file.Export(@"C:\presentation1.jpg", "JPG", 1024, 768);
Edit:Sinan's versionusing export looks to be a bit better option since you can specify an output resolution. For C#, change the last line above to:
##代码##