1

I am copying a Excel chart and pasting it into a PowerPoint slide.
After pasting the chart I want resize the chart which is pasted in image format.
Kindly help

import win32com.client 

# Grab the Active Instance of Excel.
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:\Users\ana\Python\Chart\NDC_wan_Oct24.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout
        
        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        PPTSlide.Shapes.PasteSpecial(DataType=1)

    # Save the presentation.
PPTPresentation.SaveAs(r"C:\Users\ana\Python\Chart\output")

I tried the commands below but these didn't work.

PPTSlide.Shapes(1).width = 20
PPTSlide.Shapes.items(1).width = 20
PPTSlide.Shapes.width = 20

1 Answer 1

1

The following should allow you to manipulate the chart image that is pasted into PP;
Note the additional import for Win32 constants

import win32com.client 
from win32com.client import constants

# Grab the Active Instance of Excel.
ExcelApp = win32com.client.Dispatch("Excel.Application")
ExcelApp.Visible = True

# Grab the workbook with the charts.
xlWorkbook = ExcelApp.Workbooks.Open(r'C:\Users\ana\Chart\Test_Oct24.xlsx')

# Create a new instance of PowerPoint and make sure it's visible.
PPTApp = win32com.client.gencache.EnsureDispatch("PowerPoint.Application")
PPTApp.Visible = True

# Add a presentation to the PowerPoint Application, returns a Presentation Object.
PPTPresentation = PPTApp.Presentations.Add()

# Loop through each Worksheet.
for xlWorksheet in xlWorkbook.Worksheets:

    # Grab the ChartObjects Collection for each sheet.
    xlCharts = xlWorksheet.ChartObjects()

    # Loop through each Chart in the ChartObjects Collection.
    for index, xlChart in enumerate(xlCharts):
        # Each chart needs to be on it's own slide, so at this point create a new slide.
        PPTSlide = PPTPresentation.Slides.Add(Index=index + 1, Layout=12)  # 12 is a blank layout
        
        # Display something to the user.
        print('Exporting Chart {} from Worksheet {}'.format(xlChart.Name, xlWorksheet.Name))

        # Copy the chart.
        xlChart.Copy()

        # Paste the Object to the Slide
        # PPTSlide.Shapes.PasteSpecial(DataType=1)

        ### Paste chart and resize either as a scaled from original or specific values
        img = PPTSlide.Shapes.PasteSpecial(constants.ppPasteShape)
        ### Scaled, e.g. make image 1.5 times the original
        # img.ScaleWidth(1.5, 0)
        # img.ScaleHeight(1.5, 0)
        ### Set Specific Size
        img.Width = 150
        img.Height = 150
        ### Also set position of the image in the slide
        img.Left = 100
        img.Top = 100

    # Save the presentation.
PPTPresentation.SaveAs(r"C:\Users\ana\Chart\output")

Image example
Example copy Image with no resize and after setting the Width and Height

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your kind support. its working good.
image is not changing from square shape to rectangle. Eg:img.width = 300 and img.Height = 100 not updating.
@Ana Varathan The comment is a bit vague. You're saying not changing from square shape to rectangle and not updating. The code as shown will change the size of the image in PP. Are you stating that the code makes no change to the size of the image at all? btw the attribute is Width with an upper case 'W', but I' assume that's a typo in the comments.
@Ana Varathan I have added an example image to the answer showing two PP slides of the same chart pasted from Excel. The Chart in Excel is roughly square with size 15 x 15. The image on the left is the chart with no resizing and remains a square and size 15 x 15, The image on the right has sizing applied per your comment, img.Width = 300 and img.Height = 100. As you can see its size has changed and the image is now roughtly rectangle.
put multiple image in same slide and try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.