代码之家  ›  专栏  ›  技术社区  ›  Jeremy

如何以C或Perl编程方式打开PowerPoint演示文稿并将其保存为HTML/JPEG?

  •  8
  • Jeremy  · 技术社区  · 17 年前

    我正在寻找一个能做到这一点的代码片段,最好是用C甚至Perl编写的。

    我希望这不是一个大任务;)

    2 回复  |  直到 9 年前
        1
  •  26
  •   Community Mohan Dere    9 年前

    下面将打开 C:\presentation1.ppt 并将幻灯片另存为 C:\Presentation1\slide1.jpg 等。

    如果需要获取互操作程序集,可以在Office安装程序的“工具”下找到,也可以从 here (office 2003) . 如果您有较新版本的office,您应该可以从那里找到其他版本的链接。

    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);
        }
      }
    }
    

    编辑: Sinan's version 使用导出看起来更好一些,因为您可以指定输出分辨率。对于c,将上面最后一行更改为:

    file.Export(@"C:\presentation1.jpg", "JPG", 1024, 768);
    
        2
  •  7
  •   Community Mohan Dere    9 年前

    AS 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 $0;
        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;
    }