How do I export Images and Figures from Mechanical to Powerpoint using scripting?

Options
Pierre Thieffry
Pierre Thieffry Member, Moderator, Employee Posts: 98
First Anniversary Ansys Employee Solution Developer Community of Practice Member Photogenic

I want export all figures and images I have created to Powerpoint. I want one per slide and the slide title should have the name of the parent result

Answers

  • Pierre Thieffry
    Pierre Thieffry Member, Moderator, Employee Posts: 98
    First Anniversary Ansys Employee Solution Developer Community of Practice Member Photogenic
    Options

    Here's an example how you could do it. You will have to fit to your own layout

    # Import Powerpoint Lib.
    import clr
    clr.AddReference("Microsoft.Office.Interop.PowerPoint")
    import Microsoft.Office.Interop.PowerPoint as PowerPoint
    # Create powerpoint application
    powerpoint = PowerPoint.ApplicationClass()
    powerpoint.Visible = True
    # Add presentation to the application
    presentation=powerpoint.Presentations.Add()
    # Save the presentation
    presentation.SaveAs(r"D:\temp\myppt.pptx", PowerPoint.PpSaveAsFileType.ppSaveAsDefault)
    # Choose theme to be used
    presentation.ApplyTheme(r"path_to_template\mytemplate.potx");
    
    
    # Get figures and images
    pics=[]
    
    pics.extend(ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.Image))
    pics.extend(ExtAPI.DataModel.GetObjectsByType(DataModelObjectCategory.Figure))
    
    # Pick layout from Theme (this is the style from your master slides)
    title_only_layout=presentation.SlideMaster.CustomLayouts[2] 
    
    
    for i in range(len(pics)):
        current_pic=pics[i]
        # Activate Picture/Figure and export to file
        ExtAPI.DataModel.Tree.Activate(current_pic)
        filename='d:\\temp\\Pic - '+current_pic.Parent.Name+'.jpg'
        ExtAPI.Graphics.ExportImage(filename)
    
        # Now add a slide to PPT
        presentation.Slides.AddSlide(i+1,title_only_layout)
        current_slide=presentation.Slides(i+1)
        objs=current_slide.Shapes
        # Change its title
        objs.Title.TextFrame.TextRange.Text=current_pic.Parent.Name
        # Add picture to slide
        # Top left corner (pixel)
        x=200
        y=50
        # Size
        sizex=512
        # Size as original picture
        sizey=int(sizex*ExtAPI.Graphics.Camera.ViewportHeight/ExtAPI.Graphics.Camera.ViewportWidth)
        objs.AddPicture(filename,False, True, x,y,sizex,sizey)
    
    presentation.Save()
    presentation.Close()