简体   繁体   中英

How can I manipulate shapes (colors) in PowerPoint using Python and win32com.client?

As I've made slight progress with the problem at hand since i posted this question, I've found it necessary to split it in two parts to maintain clarity.


  1. How can I manipulate shape colors in PowerPoint using Python and win32com.client?
  2. How can I inspect com objects in Python using dir() ?

1. Manipulate shape colors in PowerPoint using Python

There are some examples on how to edit PowerPoint slides using the pptx library here . However, I find it much easier to manipulate an active PowerPoint presentation using win32com.client as described here . Using an example from Microsoft Developer Network I've found that I can easily replicate parts of the functionality of this VBA snippet...

With ActivePresentation.Slides(1).Shapes(1)
    With .TextFrame.TextRange.Font
        .Size = 48
        .Name = "Palatino"
        .Bold = True
        .Color.RGB = RGB(255, 127, 255)
    End With
End With

...with this Python snippet:

import win32com.client
Application = win32com.client.Dispatch("PowerPoint.Application") 
Presentation = Application.Activepresentation

slidenr = Presentation.Slides.Count    
slide = Presentation.slides(slidenr)

shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height=100)
shape1.TextFrame.TextRange.Text='Hello, world'    

#Manipulate font size, name and boldness
shape1.TextFrame.TextRange.Font.Size=20
shape1.TextFrame.TextRange.Characters(1, 4).Font.Name = "Times New Roman"
shape1.TextFrame.TextRange.Font.Bold=True

Here I'm able to manipulate font size and name. I can also change the orientation of the textbox by changing Orientation=0x1 to Orientation=0x5 in shape1 = slide.Shapes.AddTextbox(Orientation=0x1,Left=100,Top=100,Width=100,Height=100) .

What seems impossible though, is editing the box or font color.

This does not work:

shape1.TextFrame.TextRange.Font.Color.RGB = RGB(255, 127, 255)

Error message:

在此输入图像描述

I had high hopes to fix this by importing some RGB functionality following the information on pypi.python.org

But I'm having trouble here as well with pip install colour :

在此输入图像描述

By now I'm a bit lost on all accounts, so any hints to ANY way of manipulating colors would be great!

2. Inspect objects in Python using dir()

In my attempts to manage those pesky colors, I started inspecting the output from dir(shape1.TextFrame) , dir(shape1.TextFrame.Textrange) and so on. To my disappointment, I could not find anything about colors, and not even Font, although Font is clearly accessible for manipulation.

So my second question is this: Is this not the way to inspect and manipulate these shapes at all? And how could I find the right object (or method?) to manipulate shape1 further? I have had a look at the PowerPoint objectmodel , but with little success.

Thank you for any suggestions!

You can recreate the Global VBA function easily in your python script

def RGB(red, green, blue):
    assert 0 <= red <=255    
    assert 0 <= green <=255
    assert 0 <= blue <=255
    return red + (green << 8) + (blue << 16)

About your 2nd Question. The best place to learn about these objects is the Excel Macro Object Browser. When in Macro Editor, Press F2 and then filter for Powerpoint library. Then you can search and explore the object model just related to powerpoint

对象浏览器

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM