2009-06-24 9 views

risposta

6

Come Kev punti fuori, non utilizzare questo su un server web. Tuttavia, il seguente script Perl è perfettamente bene per la conversione di file offline ecc:

#!/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 "[email protected]\n" if [email protected]; 

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

Di seguito si aprirà C:\presentation1.ppt e salvare le diapositive come C:\Presentation1\slide1.jpg ecc

Se avete bisogno di ottenere l'assembly di interoperabilità, è disponibile in "Strumenti" nel programma di installazione di Office oppure è possibile scaricarlo da here (office 2003). Dovresti essere in grado di trovare i collegamenti per altre versioni da lì se hai una versione più recente dell'ufficio.

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

Edit: Sinan's version utilizzando l'esportazione sembra essere l'opzione un po 'meglio in quanto è possibile specificare una risoluzione di uscita. Per C#, cambia l'ultima riga in alto a:

file.Export(@"C:\presentation1.jpg", "JPG", 1024, 768); 
+2

+1 per la soluzione C# (quando posso votare di nuovo che dovrebbe essere in altre otto ore o così ;-) –

+1

Questo è eccellente. Per tutti gli utenti di Office 2010 e versioni successive, la libreria "Microsoft.Office.Core" [lib è in realtà una libreria COM] (http://stackoverflow.com/questions/5932794/microsoft-office-core-reference-missing), non è un'estensione che deve essere caricata. –