1

Judging by other posts such as Python Window Activation this might not be as simple as I was hoping, but I'm compelled to ask anyway.

In the snippet below, using win32com.client and Application.Presentations.Open(ppt1, ReadOnly=0) will open and activate a powerpoint presentation. Using Application.Presentations.Open(ppt2, ReadOnly=0) will open and activate another powerpoint presentation. As you will see, I can easily reference the former presentation and do SOME things with it, but it will NOT become the active window. How can this be accomplished?

Here's what I've been working with:

# The following requires two existing presentations
# ppt1.pptx and ppt2.pptx in a directory named C:\\pptTest\\

import win32com.client

Application = win32com.client.Dispatch("PowerPoint.Application")  
directory = 'C:\\pptTest\\'
ppt_a = 'ppt_a.pptx'
ppt_b = 'ppt_b.pptx'
presentation_a = directory + ppt_a
presentation_b = directory + ppt_b

pres_a = Application.Presentations.Open(presentation_a, ReadOnly=0)
pres_b = Application.Presentations.Open(presentation_b, ReadOnly=0)


pres_a_slide1 = pres_a.Slides.Add(len(pres_a.Slides)+1, 12)
shape_a_1 = pres_a_slide1.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=50,Width=400,Height=100)
shape_a_1.TextFrame.TextRange.Text='PRESENTATION A'
##%%
pres_b_slide1 = pres_b.Slides.Add(len(pres_b.Slides)+1, 12)
shape1 = pres_b_slide1.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=50,Width=400,Height=100)
shape1.TextFrame.TextRange.Text='PRESENTATION B'

Result:

enter image description here

As you can see by the example, I can still reference the first presentation after opening another, but I can't make it the active window. The reason why I'm so interested in this, is that some methods will not work properly unless the presentation I'm editing through Python is also the active window. I'll get into those details as well if that is interesting to anyone.

Thank you for any suggestions!

vestland
  • 55,229
  • 37
  • 187
  • 305
  • 1
    I've used autoIt in the past to switch the focus between applications. Perhaps that's something that can help you with your question. – Chuk Ultima Feb 15 '18 at 10:58

2 Answers2

1

I dug through some old code and found something where I also had to switch between different open programs :

from win32com.client import Dispatch
autoit = Dispatch("AutoItX3.Control")

def _window_movement_windows(page_title):
        autoit.WinSetOnTop(page_title, "", 1)
        autoit.WinActivate(page_title, "")
        autoit.WinWaitActive(page_title)

An example how to setup AutoIt with python can be found here : Calling AutoIt Functions in Python

Chuk Ultima
  • 987
  • 1
  • 11
  • 21
  • Thank you for answering! pip install -U pyautoit first? Or some other approach? – vestland Feb 15 '18 at 12:33
  • 1
    It's been too long since I set it up, but I quickly googled it and here's a way to set it up : https://pypi.python.org/pypi/PyAutoIt/0.3 – Chuk Ultima Feb 15 '18 at 12:42
1

Several examples, see if that works for you

import win32com.client as win32

file1='C:/test.pptm'
file2='C:/test2.pptm'

PowerPoint=win32.DispatchEx("PowerPoint.Application")
PowerPoint.Visible = True
Presentation1 = PowerPoint.Presentations.Open(file1)
Presentation2 = PowerPoint.Presentations.Open(file2)

PowerPoint.Windows(1).Activate()
PowerPoint.Windows(2).Activate()

print(PowerPoint.ActivePresentation.Name) # to see the name
PowerPoint.Presentations("test.pptm").Windows(1).Activate()
PowerPoint.Presentations("test2.pptm").Windows(1).Activate()

Presentation1.Windows(1).Activate()
Presentation2.Windows(1).Activate()
coldbyte
  • 66
  • 3