Automating Powerpoint with ruby

Posted on October 9, 2007. Filed under: powerpoint, ruby, win32ole, windows | Tags: , , , , |

We know that ruby is a language of few words. We can express a lot of things in a relatively easy manner.

I was trying to export few slides in my powerpoint presentation to an image format using ruby.I could not find any documentation for it anywhere except for a few basic things. But when I tried it by hit and trial I found out it to be surprisingly easy. It required just a few lines.

Here is what you have to do:

   1: require ‘win32ole’
   2: # open powerpoint
   3: ppt = WIN32OLE.new(‘Powerpoint.Application’)
   4: # make sure it is visible
   5: ppt.Visible = true
   6: # open the presentation to be exported
   7: pre = ppt.Presentations.Open(“d:\\imp_file.pptx”)
   8: # export the file
   9: pre.Slides(1).Export(“d:\\exp_file.png”,“png”)
  10: # close powerpoint, will close all the currently open files
  11: ppt.Quit()

You can always use RMagick to further process this image.

The Win32OLE extension library (actually spelled in lowercase, win32ole) provides an interface to Windows OLE automation. Your Ruby code can act as a client for any OLE automation server such as Microsoft Word, Outlook, and Internet Explorer and many other softwares.

So in the above code we are making a object is Powerpoint application, then opening a file and saving the first slide in the “png” format.

Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.