Open XML – 1, PowerPoint UI – 0

In my very first TechieSimon article, I went on about how great Open XML was. Today I’m going to prove it – by showing how a month ago, I was able to use Open XML to make an edit to a PowerPoint document that I couldn’t easily do in PowerPoint.

At the time I was checking through some of the slides for my Math for Programmers Pluralsight course, and discovered I’d used two wrong images throughout one of my .pptx files. For example, this picture shows one of the slides from the document – what it should have looked like, and what actually was there.

OpenXML1 Ppt0_old-and-new-slides

The wrong images were the images of ‘1’ and ‘0’. You might not think that using a different image for the numbers ‘1’ and ‘0’ was a big deal but in the course I’d be showing a lot of numbers in binary, decimal, hexadecimal, and other bases, and to avoid confusing people, I intended to use the type face to distinguish the bases. The green 0 and 1 was what I was supposedly using to indicate decimal numbers, so putting them in a binary number could probably be rather confusing. I needed to swap them out.

You may think it should be an easy matter to replace a few images, but the trouble is these images occur throughout the document – between them, well over 100 times. Replacing all those individually would be a very tedious task. Not only that, but they often participated in various animations which it had taken me ages of quite fiddly work to set up. For the sake of showing off, here’s a link to one of the animations on youtube.

In short, I needed to do a global replace of over 100 occurrences of these images without affecting the animations they were in. Now PowerPoint does offer a way to replace single images without affecting animations – you have to double click each image – this brings up an edit image ribbon, with a Change Picture button on it. But as far as I could tell there was no way – short of writing a macro – to do this to multiple images at once. Maybe there is a hidden trick to doing that and someone will embarrass me 5 minutes after this article goes live by posting a comment explaining how to do it. But if there is, I sure couldn’t find it.

If I didn’t know about Open XML, that would have been a pretty depressing afternoon. But luckily I do know about Open XML.

Here’s what I did:

First, I renamed the file from its original name of 6-interpreting-bits.pptx to 6-interpreting-bits.pptx.zip. Actually that was the 2nd thing I did. The first thing I did was take a backup of the file, but I don’t need to tell you that, do I. Then I right-clicked on it and told Windows Explorer to unzip it.

OpenXML1 Ppt0_Extract

This works because Office docx etc. files are literally zipped up archives, so it’s easy to unzip them and explore the file structure.

If you navigate into an unzipped PowerPoint, you’ll normally find the slides in the ppt/slides subfolder in the zip. Here’s the one I was playing with.

OpenXML1 Ppt0_slides

Now what I wanted to do is replace all the images while leaving everything undisturbed. This is remarkably easy to do in Open XML because the images are indirectly linked rather than embedded. In fact we don’t need to touch the slides XML files at all. Our destination is that _rels subfolder. The idea of _rels is that each slide has an associated .rels file in the _rels folder, which describes the relationship between the slide and any media, images or other stuff embedded in that slide. Here they are:

OpenXML1 Ppt0_rels-folder

Let’s look at the .rels file for slide 5, which is the one I showed the screenshot of earlier.

OpenXML1 Ppt0_inside-rels

It mentions two images, image7.png and image8.png. Presumably they are the images for ‘1’ and ‘0’ since those are the only two images used on that slide. The way it works is that wherever slide 5 needs to embed – say – image8.png, the XML in slide5.xml will use the identifier rId3. PowerPoint will then look up the Id rId3 in the .rels file, which as you can see says that rId3 is an image (the Type attribute), and precisely where in the zip archive it can find the image (the Target attribute). Neat, huh!

In fact this means ideally I don’t need to touch the .rels file either: Since I know I want to replace the image *[b]everywhere[/b]* in the presentation, the easiest thing is to open up that folder that I’ve just read out of the .rels file, and replace the files with the images I want. Here’s what I see when I go to the path.

Open XML1 Ppt0old-images

Uh oh. It’s not just a case of replacing one ‘0’ image and one ‘1’ image. It looks like PowerPoint has put four copies of the ‘0’ image and 3 copies of the ‘1’ in the file. There’s also a Pluralsight background image which isn’t actually used. That’s all harmless, but a bit of a waste of space – and bandwidth when people download the presentation.

Simply replacing all seven ‘0’ and ‘1’ images would be trivial at this point. All I need to do is delete those images, from the folder, and replace them with the new ones, being careful to keep the same file names – if I did that, I would not need to make any other changes at all, since PowerPoint would automatically find the new images when it looked up their paths in the ._rels files. However, the perfectionist in me immediately wants to remove the duplicates and save the 0.0001 seconds or so of bandwidth, so in fact I deleted all the images and put just two replacements in there. Now the folder looks like this.

OpenXML1 Ppt0_new-images

Of course, now I’ve removed the files image3.png, image4.png, etc., I’ll need to touch up the .rels files to make sure the images they refer to actually exist, otherwise PowerPoint will not be happy. So for example here’s what the .rels file for slide 5 looks like, with my edits highlighted

And that’s literally it. Now I’ve replaced those files, all that remains is to zip up the new folder structure – literally, create a new zip file in Windows Explorer, and rename the extension from .zip to .pptx, and that’s it. A working PowerPoint presentation, with the images replaced en masse. And because I never touched the animations during my Open XML editing, those will be intact too. And to prove it, here’s a youtube link to the earlier animation, now with the new images.

Like the title says. In this game Open XML won hands down. If I liked soccer, I might even consider titling this article Open XML – 1, PowerPoint UI – 0, but as it happens I don’t follow soccer so I probably shouldn’t do that. Umm – oh dear…

Advertisements

One thought on “Open XML – 1, PowerPoint UI – 0

Comment on this article (your first comment on TechieSimon will be moderated)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s