Search code examples
pythonms-officeoffice-interopwin32com

Is there anyway to know what object does the file have in MS powerpoint or word


I use win32com to handle office document in Python. There is no way to know what method does an Object have in win32com. (In general, we can fetch properties or methods by DIR(OBJ) in Python ) So, if we want to know what properties or methods does my powerpoint document have , just go to look up MSDN

however , I have a powerpoint file contains lots of object. but i don't know what name or type is it.

for example ,when i want to access text in powerpoint i can use Presentation.Slide.Textframe.TextRange.Text to access it.

How about formula??? if i want to access it? Is there anyway when I click mouse on the object in my powerpoint file, and then show what type of the object is?

Thanks you all in advance.


Solution

  • For your first question you want to use the MakePy Utility to create early-bound objects so you can introspect them (ie. press tab and look at all the methods in your IDE). It will cut down searching on MSDN by 95%

    Easiest way to do this is go to PythonWin (installed with win32com) and go to Tools > COM Makepy Utility and select the COM library you want to use (in your case it is something like 'Microsoft PowerPoint 14.0 Object Library'). Let this run and you are all set. This is also described here.

    For your second question, as David pointed out most of the objects are Shapes. Once you run the MakePy Utility you will be able to see the entire PP Object Model. A quick search helped me find how to get the active shape that is selected by clicking on it.

    import win32com.client
    app = win32com.client.Dispatch("PowerPoint.Application")
    
    selectedShape = app.ActiveWindow.Selection.ShapeRange(1)
    

    You can now play around with selectedShape to find out everything you need to know about it.