Posts Tagged Powerpoint

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: , , , ,

Use python to create PowerPoint slides automatically

I have a large number of images. I want to organize them in powerpoint files by automatically insert each picture to a new powerpoint slide and control it’s position and size. I came across a nice Python library – python-pptx at https://python-pptx.readthedocs.io/en/latest/. It provides functions to automate generating PowerPoint slides. Below are some information from the website. I keep the information for future use.

python-pptx is a Python library for creating and updating PowerPoint (.pptx) files.

A typical use would be generating a customized PowerPoint presentation from database content, downloadable by clicking a link in a web application. Several developers have used it to automate production of presentation-ready engineering status reports based on information held in their work management system. It could also be used for making bulk updates to a library of presentations or simply to automate the production of a slide or two that would be tedious to get right by hand.

Chris Moffitt wrote a nice article to explain how to use the above library at http://pbpython.com/creating-powerpoint.html. He specifically talked about how to use PowerPoint templates to generate new slides.

 

Share

Tags: , , , ,

Embed Movie, Flash, Music files to PowerPoint Slides

Recently I switched to Microsoft Office 2007. User interface has been changed dramatically. I had a hard time to get familiar with the Office components. Even though, I managed to learn them very fast and got things done quickly. The article provides several lessons I learned to deal with media files in PowerPoint slides. I simply have three categories of media files: music, movie, and flash movie files.

Embed music

Embedding a music file into slides is easy. Click the arrow button under the Sound icon in the insert ribbon, a drop-down menu shows up (Figure 1). Click “Sound from file” to open and insert a music file.

Figure 1

The tricky part start once a music file is inserted into a slide. We click anywhere to play the music file play once the slide shows. Click “Custom Animation” button in the Animation ribbon, a side window shows on the right side. Select the music file icon in the slide, click the drop-down arrow in the start box and select “After previous” (Figure 2).

Figure 2

There are more setting options available in the context menu (Figure 3). Click “Effect options” to open a dialog window (Figure 4). One key function is in the timing tab. Click the “Triggers” button and select “Animate as part of click sequence”. If the music does not automatically play, please check this option.

Figure 3

Figure 4

Embed movies

VLC player is a universal movie player. It can play movie formats that other player cannot do. So you can download and install VLC player to your computer first.

If your movie format is .MWV, you can just follow a similar procedure as we insert music files into slides and custom animation as you want. If your movie files are other format, please use the following method to insert it for the VLC player.

Before you can insert VLC player object, you have to enable the Developer tab in the Ribbon. Click “PowerPopint options” button at the button of the control menu (Figure 5) to open the option dialog (Figure 6).

Figure 5

In the “Popular” page, select “Show Developer tab in the Ribbon” option, click “Ok” to enable it in the main menu.


Figure 6

Click the “More Control” button in the Developer tab in the Ribbon, to open the More Control dialog (Figure 7). Find “VideoLAN VLC ActiveX Plugin v1” in the list and click “Ok”. Use your mouse to place a rectangular box to your slide.


Figure 7

Figure 8 show a rectangular box fill the whole slide with an icon in the middle.


Figure 8

Right click the box and click “Properties” in the context menu, a Properties dialog shows up (Figure 9). What you should do is to enter the movie file with correct path (either absolute or relative path) in the MRL field.


Figure 9

After you complete these steps, you can show the slide and see if the desired effect is achieved. The movie should play automatically when the slide shows.

Embed flash movies

Flash movie is a special movie format that requires a special player – Shockware Flash player. The Shockware Flash player can be found in the More Control list (Figure 10).


Figure 10

Once you select “Shockware Flash object” in the More Control list, you can create a rectangular box in the slide. Then right click the box to show the context menu. Click “Properties” item to open the Properties dialog.  Enter the flash movie file name with correct path (either absolute or relative path) in the “Movie” field (Figure 11).


Figure 11

Show the slide and test if the movie play or not. In the slide show mode, right click the movie to popup the Shockware Flash Player context menu (Figure 12). From this menu can control the flash movie show.

Figure 12

This is what I learned when I prepared slides for a large party with a lot of different media formats. Please post comments or contact me if you find errors or new suggestions.

Share

Tags: , , , , , , , , , , , , , , ,

Create PowerPoint slide in C#

There are situations we need control MS Office components in our program, for example, manipulate data in Excel workbook, generate PowerPoint slides, intracte with Access database etc. In this article, I will provide detail information about how to control PowerPoint in C# program.

First of all, how can use use MS Office components in our standalone program in general? Since MS Office provide Office Primary Interop Assemblies, for example, Microsoft.Office.Interop.Excel and Microsoft.Office.Interop.PowerPoint. In you program that intends to control MS Office components, you have to add reference to these interop assemblies. To contol MS PowerPoint, you need add a reference pointing to Microsoft.Office.Interop.PowerPoint. You can use Add Reference function the GUI environment to achieve this.

The following code snippets provide some methods to control PowerPoint, mainly create new slides automatically. It is a method in a large program and is not a generic procedure. You can use ideas and methods presented in the code to generate yours.

Code snippet

private void CreatePresentation()
{
	string BasePath = Path.GetDirectoryName(myFilename) + "\\";
	string BaseFilename = "";

	String strTemplate = BasePath + "Calendar.ppt";
	//bool bAssistantOn;

	PowerPoint.Application objApp;
	PowerPoint.Presentations objPresSet;
	PowerPoint._Presentation objPres;
	PowerPoint.Slides objSlides;
	PowerPoint._Slide objSlide;
	PowerPoint.TextRange objTextRng;

	//Create a new presentation based on a template.
	objApp = new PowerPoint.Application();
	objApp.Visible = MsoTriState.msoTrue;
	objPresSet = objApp.Presentations;
	objPres = objPresSet.Open(strTemplate, 
		MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
	objSlides = objPres.Slides;

	//Build Slide #1:
	//Add text to the slide, change the font and insert/position a 
	//picture on the first slide.
	objSlide = objSlides.Add(1,PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
	objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
	objTextRng.Text = "Remote sensing calendar";
	objTextRng.Font.Name = "Comic Sans MS";
	objTextRng.Font.Size = 48;

	// insert a PowerPoint table
	// use data from a file
	PowerPoint.Table objTable = null;
	using (StreamReader sr= new StreamReader("summary.csv"))
	{
		int nLine = 1;
		int nStartLine = 0;
		while (!sr.EndOfStream)
		{
			string line = sr.ReadLine();
			if (line.Trim() == "")
			{
				if (nStartLine == 0) 
				{
					nStartLine = nLine;
					objTable= objSlide.Shapes.AddTable(nStartLine-1,9,36,138,648,294).Table;
				}
				else
				{
					break;
				}
			}
			if ((nStartLine > 0) && (nLine>nStartLine))
			{
				string [] cells = line.Split(',');
				for (int i=1; i<=cells.Length; i++)
				{
					int k = i;
					if (i>1) k = i+2;
					objTable.Cell(nLine-nStartLine,k).Shape.TextFrame.TextRange.Text = cells[i-1];
					objTable.Cell(nLine-nStartLine,k).Shape.TextFrame.TextRange.Font.Size = 12;
				}
			}
			nLine++;
		}
	}

	// build more slides
	// add pictures to the slides
	for (int i=0; i=1) System.Threading.Thread.Sleep(100);

//Reenable Office Assisant, if it was on:
if(bAssistantOn)
{
	objApp.Assistant.On = true;
	objApp.Assistant.Visible = false;
}
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: , , ,