简体   繁体   English

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

[英]How can I programmatically open and save a PowerPoint presentation as HTML/JPEG in C# or Perl?

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 ;) 我希望这不是一项大任务;)

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) . 如果需要获取互操作程序集,可以在Office安装程序的“工具”下找到它,也可以从此处下载(office 2003) You should be able to find the links for other versions from there if you have a newer version of 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);
    }
  }
}

Edit: Sinan's version using export looks to be a bit better option since you can specify an output resolution. 编辑:使用导出的Sinan版本看起来是更好的选择,因为您可以指定输出分辨率。 For C#, change the last line above to: 对于C#,将上面的最后一行更改为:

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

As Kev points out, don't use this on a web server. 正如Kev所指出的,不要在Web服务器上使用它。 However, the following Perl script is perfectly fine for offline file conversion etc: 但是,以下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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#/ Powerpoint加载项如何将datagridview保存到演示文稿中? - C#/Powerpoint Add-in How can I save a datagridview to the presentation? 如何使用C#在Powerpoint演示文稿中打开嵌入式Excel工作表 - How to open embedded excel sheet in Powerpoint presentation using c# 使用C#反射或perl自动打开OpenOffice Impress格式并将其保存为html / jpeg - Automate open and save OpenOffice Impress format as html/jpeg using C# reflection, or perl 以幻灯片模式打开 PowerPoint 演示文稿 - C# - Open PowerPoint presentation in slideshow mode - C# 如何在 ASP.NET MVC C# 项目的新选项卡中打开 PowerPoint 演示文稿 - How to open PowerPoint Presentation in New Tab in ASP.NET MVC C# project 我可以在PowerPoint Presentation属性上启用“保存预览图片”吗? - Can I enable “Save Preview Picture” on PowerPoint Presentation properties 我可以在形状中添加超链接以关闭C#中的当前PowerPoint演示文稿吗 - Can I add a hyperlink to my shape to close the current powerpoint presentation in C# 如何使用 C# 以编程方式将 RTF 文本插入到 PowerPoint 文本框架中? - How can I programmatically insert RTF text into a PowerPoint text frame using C#? 如何使用 Visual Studio C# 处理 PowerPoint 演示文稿? - How do I work with a PowerPoint presentation using Visual Studio C#? C#如何获取当前的Powerpoint演示文件名称? - C# How to get current Powerpoint Presentation File Name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM