Posts Tagged Slide

Generate PowerPoint slides in R

A new R package “officer” makes generating PowerPoint slides in R much easier. It is newly developed and still has bugs. But there are ways to get around the bugs. I believe the developer of the package will fix bugs quickly.

First, you have to install the package before using it.

install.packages("officer")

Second, use the examples in the GitHub site and other sites as the start point to create your own PowerPoint slide. I found this site (http://lenkiefer.com/2017/09/23/crafting-a-powerpoint-presentation-with-r/) is really useful.

Third, get around bugs. When I used it to automate the generation of PowerPoint slides, I found that all slides with images inserted did not work at all. After PowerPoint files were generated, they cannot be open correctly. Always popup error message and let you “repair” the presentations. I searched several hours and did not get any useful information. However, I visited “officer” package’s GitHub page and found a hit in bug submitting area. The reason popping up error message is a bug in the program. All image filenames cannot include “SPACE”, otherwise, you get error.

So, I went ahead and changed my program to make sure there is no “space”s in any image filenames. Waoh-lah, it works great.

Another useful point is the slide size. If you want to insert an image to occupy the whole 9:16 widescreen slide, you should use the following parameters:

left = 0.0
top = 0.0
width = 40.0/3.0
height = 7.

Use this set of parameters in ph_with_img_at function, you can guarantee inserted images take the whole slide.

Enjoying to use the package. Thank the developer sharing the wonderful art of work with us.

Share

Tags: , , , ,

Automate PowerPoint Slide Creation

One time I have to generate a lot of graphs on fly and insert them to PowerPoint slides to create a nice presentation. The situation is that I have to repeat this process again and again every week or days. The graphs generated on fly are a fixed set. Nobody want to do this boring work again and again. So I developed a VBA script in PowerPoint. I can use it to automate the process. Here are some pieces that are relevant to the auntomatic process.
First of all, we need insert new slide.

' create a new blank slide
ActiveWindow.View.GotoSlide Index:=ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutTitle).SlideIndex
ActiveWindow.Selection.SlideRange.Layout = ppLayoutBlank 

Second, we insert pictures to each slide and put them in fixed position precisely.

ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="picture1.jpg", LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, left:=12, top:=12, Width:=770, Height:=690).Select
' set size of picture
With ActiveWindow.Selection.ShapeRange
.Fill.Transparency = 0#
.Height = 262.38
.Width = 293#
.left = 100#
.top = 50#
End With 

Third, you may need insert textboxes to the slide. Here is sample code.

ActiveWindow.Selection.SlideRange.Shapes.AddTextbox(msoTextOrientationVerticalFarEast, 29.875, 84, 36.125, 348).Select
ActiveWindow.Selection.ShapeRange.TextFrame.WordWrap = msoTrue
With ActiveWindow.Selection.TextRange.ParagraphFormat
.LineRuleWithin = msoTrue
.SpaceWithin = 1
.LineRuleBefore = msoTrue
.SpaceBefore = 0.5
.LineRuleAfter = msoTrue
.SpaceAfter = 0
End With
ActiveWindow.Selection.ShapeRange.ScaleHeight 0.93, msoFalse, msoScaleFromBottomRight
ActiveWindow.Selection.TextRange.ParagraphFormat.Alignment = ppAlignCenter
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Characters(Start:=1, Length:=0).Select
With ActiveWindow.Selection.TextRange
.Text = "Sample graphes"
With .Font
.NameAscii = "Arial"
.Size = 18
.Bold = msoFalse
.Italic = msoFalse
.Underline = msoFalse
.Shadow = msoFalse
.Emboss = msoFalse
.BaselineOffset = 0
.AutoRotateNumbers = msoFalse
.Color.SchemeColor = ppForeground
End With
End With
ActiveWindow.Selection.ShapeRange.TextFrame.TextRange.Font.Bold = msoTrue
ActiveWindow.Selection.Unselect 

If you need move to slide to slide, the following code can do that.

ActiveWindow.View.GotoSlide (i) 

In the second step, when we position graphs, we need pixel units instead of inches PowerPoint uses. To convert inches in PowerPoint slide to pixels, the following formula can be used:

number in inches / 2.54 * 72 

By using this formula, you can precisely position your graphs to slides.

Share

Tags: , , ,