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
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")