Slides from Ruby
A PowerPoint file (.pptx) is really a ZIP archive full of XML files – one per slide, plus layouts, themes and images. Nobody writes that by hand, but with a gem it becomes easy: ruby_pptx is a Ruby port of the well-known Python library python-pptx.
Everything here runs in the browser: the gem is installed, the presentation is built and saved in Ruby. Every file your cell saves shows up below it as a download – click it and open it in PowerPoint, Keynote or LibreOffice.
install_gem "ruby_pptx"
require "ruby_pptx"
deck = Pptx::Presentation.new_default
title = deck.slides.add(deck.slide_layouts["Title Slide"])
title.shapes.title.text = "Chunky Bacon"
title.placeholders[1].text_frame.text = "A presentation, built with Ruby"
deck.save("chunky.pptx")What just happened?
Presentation.new_defaultcreates an empty presentation from the default template.- Every slide is based on a layout, just like in PowerPoint when you click “New Slide”.
slide_layouts["Title Slide"]finds one by name. - A layout brings placeholders:
shapes.titleis the title,placeholders[1]the box below it. You write text into theirtext_frame. savewrites the file – and it is ready to download below.
Which layouts are there? Just ask – the cell still knows deck from above:
deck.slide_layouts.map(&:name)For bullet points use "Title and Content". Each line (separated by \n) becomes a paragraph with its own bullet. A paragraph has a level for indentation, and its pieces of text (runs) have a font – bold, italic, size, colour.
slide = deck.slides.add(deck.slide_layouts["Title and Content"])
slide.shapes.title.text = "Breakfast"
text = slide.placeholders[1].text_frame
text.text = "Bacon\nnice and crispy\nEggs\nToast"
text.paragraphs[1].level = 1
text.paragraphs[0].runs[0].font.bold = true
deck.save("chunky.pptx")On an empty slide ("Blank") you place shapes yourself. Positions and sizes are real lengths – no bare numbers where you have to guess whether pixels or centimetres are meant. at: is the top-left corner, size: width and height.
It should read like Ruby: 2.cm, 1.inch, 28.pt. For that the gem ships a refinement – an extension of a built-in class, here Numeric, that only applies where you switch it on with using. Unlike a monkey patch, which changes Ruby for everyone, it cannot get in the way of any other library. Without it you write Pptx.cm(2) – the same thing, just longer.
Colours are hex codes, as in CSS: "E8722A" is Chunky Bacon orange.
require "ruby_pptx/refinements"
using Pptx::Lengths
slide = deck.slides.add(deck.slide_layouts["Blank"])
box = slide.shapes.add_shape(:rounded_rectangle,
at: [2.cm, 2.cm],
size: [12.cm, 4.cm])
box.fill.solid
box.fill.fore_color.rgb = "E8722A"
box.text_frame.text = "Chunky Bacon!"
box.text_frame.paragraphs[0].runs[0].font.size = 28.pt
deck.save("chunky.pptx")And charts? The numbers go into a ChartData: categories for the x axis, plus one or more series. The gem even stores the Excel sheet behind the chart – in PowerPoint you can edit the data later.
data = Pptx::ChartData.new
data.categories = %w[Mon Tue Wed Thu Fri]
data.add_series("Bacon", [3, 5, 2, 6, 4])
slide = deck.slides.add(deck.slide_layouts["Title Only"])
slide.shapes.title.text = "Bacon per day"
slide.shapes.add_chart(:column_clustered, data,
at: [2.cm, 4.cm],
size: [20.cm, 12.cm])
deck.save("chunky.pptx")Reading works too: Presentation.open opens an existing file, and you walk through its slides like an array. The blank slide has no title – hence &., which simply returns nil for nil instead of crashing.
opened = Pptx::Presentation.open("chunky.pptx")
opened.slides.map { |s| s.shapes.title&.text }gem install ruby_pptx – or gem "ruby_pptx" in your Gemfile. There the gem reads and writes XML with Nokogiri, a fast C extension. The browser has no C extensions, so here it automatically uses REXML, written in pure Ruby: slower, but producing exactly the same file. On your machine you can save anywhere, e.g. deck.save("~/Desktop/chunky.pptx").menu with a title slide and at least two more slides – each with a title, e.g. “Starters” and “Mains” with a few dishes as bullet points. Save it as menu.pptx.# menu = Pptx::Presentation.new_default
# title = menu.slides.add(menu.slide_layouts["Title Slide"])
# ...
# menu.save("menu.pptx")